1
struct E_Point {
  double x, y;
  E_Point(): x(0), y(0) {}
  E_Point(double x, double y) : x(x), y(y) {}
};

E_Point problem[] = { 
  {0.3871953044519425, -0.91857980824611341}, // Error: initialization of non-aggregate type 'E_Point' with an initializer list
  {0.36139704793723609, 0.91605957361605106}, 
  {-0.8208980020500205, 0.52853407296583088}, 
  {0.36178501611208552, 0.88880385168617226}, 
  {-0.43211245441046209, 0.6803420222771047} 
};

Compiling with clang 3.1.

I should point out that this compiles on GCC 4.6.1.

My current understanding is that problem is a non-aggregate type because it is an array of structs, making it compound and not just simply either a struct or array.

But sending the -std=c++11 flag to clang does not alleviate the problem.

Update: Okay. Looks like my version of clang is somehow flawed and can't handle this for whatever reason.

What's a more traditional way to init? Do I do it this way? This compiles, but does it produce the same code? Will it call the ctor while the original doesn't?

E_Point problem[] = {
    E_Point(0.3871953044519425, -0.91857980824611341), // 1559
    E_Point(0.36139704793723609, 0.91605957361605106), // 1560
    E_Point(-0.8208980020500205, 0.52853407296583088), // 1798
    E_Point(0.36178501611208552, 0.88880385168617226), // 1799
    E_Point(-0.43211245441046209, 0.6803420222771047) // 1800
};
20
  • Time to switch the compiler? Compiles file with gcc 4.3.4 Commented Jun 6, 2012 at 2:53
  • 2
    Yes it does have a additional flag perhaps you have not enabled it, something like clang++ -std=c++11 [files], By default clang does not enable c++11 features, You have to enable explicitly. Commented Jun 6, 2012 at 2:56
  • 3
    @Als: That isn't a C++11 feature; this is basic C++98 stuff. That's pure aggregate initialization, and it ought to work. Commented Jun 6, 2012 at 3:01
  • 2
    BTW, your understanding is incorrect, an array of aggregates is an aggregate itself. The code is correct as is and it compiles with clang++ 3.1 (tags/Apple/clang-318.0.54) [MacOX Lion] Commented Jun 6, 2012 at 3:11
  • 2
    @StevenLu: Your struct that in the question here is an aggregate in your real code is not an aggregate. Everything else in the question is just a lost of time, as it is based on the premise that the original type is what it is not. E_Point is not an aggregate, you cannot use aggregate initialization. If you are using a C++11 compiler things change, as uniform initialization can be used there, and the inner curly braces {} will be mapped to a call to the constructor. What exact version of clang++ are you using? Commented Jun 6, 2012 at 3:37

1 Answer 1

2

That is a bug in clang++:

http://llvm.org/bugs/show_bug.cgi?id=12670

In your case you can just use the explicit call to the constructor, as you provide in the last code snippet. As of whether the semantics are really the same (will it generate the same code), in most cases it will.

The case where the different syntax will lead to different behavior is when the type being constructed has a constructor that takes a std::initializer_list<>, in which case the brace-initializer will construct that initializer list. If the type does not have such a constructor, as is your case, the brace-initializer will call the appropriate constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.