Clang++ non-static data member initialization error? C++11

1k views Asked by At

I can't seem to figure out what Clang is saying or whether it's right as G++-4.7 seems to compile it fine.

The error comes from trying to initialize std::uniform_int_distribution with curly braces for a non-static member.

The following fails (token_count is a template parameter): std::uniform_int_distribution<Int> random_dist{0, token_count-1};

with the error:

error: chosen constructor is explicit in copy-initialization
  std::uniform_int_distribution<Int> random_dist{0, b-1};
                                                ^~~~~~~~

/usr/include/c++/v1/algorithm:2644:14: note: constructor declared here
    explicit uniform_int_distribution(result_type __a = 0,

I can, however, initialize it by doing this:

std::uniform_int_distribution<Int> random_dist = std::uniform_int_distribution<Int>(0, token_count - 1);

I am using the following command to compile it: clang++ -std=c++11 -stdlib=libc++ -lc++abi with Clang-3.2.

Output of clang -v:

clang version 3.2 (trunk 157320)
Target: x86_64-unknown-linux-gnu
Thread model: posix
1

There are 1 answers

4
Howard Hinnant On

You probably have a version of clang that does not yet implement generalized initializers. Tip-of-trunk clang compiles your code. You can check for this feature with:

#if __has_feature(cxx_generalized_initializers) 

Here's the list of features you can check for:

http://clang.llvm.org/docs/LanguageExtensions.html#cxx11