C ++ 11 Unified Initialization Doesn't Work with clang ++

I copied the following example from this wikipedia page :

struct BasicStruct { int x; double y; }; struct AltStruct { AltStruct(int x, double y) : x_{x}, y_{y} {} private: int x_; double y_; }; BasicStruct var1{5, 3.2}; AltStruct var2{2, 4.3}; int main (int argc, char const *argv[]) { return 0; } 

Then I tried to compile it with

 clang++ -Wall -std=c++11 test.cpp 

but I get this error:

 test.cpp:17:11: error: non-aggregate type 'AltStruct' cannot be initialized with an initializer list AltStruct var2{2, 4.3}; ^ ~~~~~~~~ 1 error generated. 

My version of clang clang++ --version is

 Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.0 Thread model: posix 

Should this example not work? Maybe clang is just not fully compatible with C ++ 11?

What's happening?

+4
source share
2 answers

The hint is actually here:

 Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn) ^~~~~~ 

This means that this is not version 3.1, but somewhere between 3.0 and 3.1.

Support for uniform initialization was implemented somewhere between the two versions, so the version that Apple probably either does not support at all, or simply partially supports.

+7
source

Error: the non-aggregate type "AltStruct" cannot be initialized with the initialization list

This is definitely not true, see 8.5.4 List-initialization [dcl.init.list] ยง3:

An initialization list of an object or link of type T is defined as follows:

  • If T is an aggregate, aggregate initialization is performed
  • [...]
  • Otherwise , if T is a class type, the constructors are considered

And since AltStruct has a constructor that accepts int and double , AltStruct var2{2, 4.3}; must compile and have the same semantics as AltStruct var2(2, 4.3); (direct initialization).

0
source

All Articles