2

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
norcalli
  • 1,215
  • 2
  • 11
  • 22
  • 1
    Can you give us the exact output from `clang -v`? – Xeo May 26 '12 at 21:16
  • Perhaps you could use the old-school `std::uniform_int_distribution random_dist(0, token_count-1)`. – kennytm May 26 '12 at 21:34
  • This [bug](http://llvm.org/bugs/show_bug.cgi?id=12120) *may* be the relevant one, but I'm not sure. – Jesse Good May 26 '12 at 22:05
  • When it comes to non-static data member initialization, only `=` or `{}` may be used to initialize. – norcalli May 26 '12 at 22:55
  • What is `Int` and `token_count`? Apart from that it works for me -- clang version 3.2 (trunk 157115). – dirkgently May 26 '12 at 23:07
  • Int is just an alias for the smallest integer capable of holding a template parameter called token_count. I updated to clang 3.2 (trunk 157522). Are you using libc++? – norcalli May 26 '12 at 23:57

1 Answers1

1

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

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • 1
    Well, he does say that he used Clang 3.2, which I would find a bit strange to not support generalized initializers, since those were already working in Clang 3.1. I can, however, confirm that Clang 3.2 r156775 definitely compiles the code. – Xeo May 26 '12 at 21:38
  • 2
    I'm pretty sure it was a recent bug fix. – Jesse Good May 26 '12 at 21:40
  • Perhaps we also need: __has_feature(cxx_nonstatic_member_init) – Howard Hinnant May 26 '12 at 21:41
  • Just tested `cxx_generalized_initializers` and `cxx_nonstatic_member_init`, and both work. – norcalli May 26 '12 at 23:03