Initializing simple structs using initializer list

2019-08-04 00:15发布

问题:

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
};

回答1:

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.