aggregate-initialization

Brace elision in std::array initialization

[亡魂溺海] 提交于 2019-11-26 16:23:36
问题 Suppose there's an std::array to be initialized. It's okay if using double braces: std::array<int, 2> x = {{0, 1}}; std::array<int, 2> x{{0, 1}}; It's also okay to use single braces in the good old aggregate initialization, as the brace elision will take care of the missing braces: std::array<int, 2> x = {0, 1}; However, is it okay to use list-initialization with single braces? GCC accepts it, Clang rejects it with "cannot omit braces around initialization of subobject when using direct list

Initializing a member array in constructor initializer

廉价感情. 提交于 2019-11-26 14:31:01
class C { public: C() : arr({1,2,3}) //doesn't compile {} /* C() : arr{1,2,3} //doesn't compile either {} */ private: int arr[3]; }; I believe the reason is that arrays can be initialized only with = syntax, that is: int arr[3] = {1,3,4}; Questions How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor initializers? Or the invalidness of the above code is a corollary of some other rules? Do C++0x initializer

C++11 aggregate initialization for classes with non-static member initializers

て烟熏妆下的殇ゞ 提交于 2019-11-26 11:22:16
Is it allowed in standard: struct A { int a = 3; int b = 3; }; A a{0,1}; // ??? Is this class still aggregate? clang accepts this code, but gcc doesn't. In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no one really expects that adding in-class initializers should make their class/struct a non-aggregate, I sure did

Narrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?

独自空忆成欢 提交于 2019-11-26 07:28:32
问题 C++0x is going to make the following code and similar code ill-formed, because it requires a so-called narrowing conversion of a double to a int . int a[] = { 1.0 }; I\'m wondering whether this kind of initialization is used much in real world code. How many code will be broken by this change? Is it much effort to fix this in your code, if your code is affected at all? For reference, see 8.5.4/6 of n3225 A narrowing conversion is an implicit conversion from a floating-point type to an integer

Deleted default constructor. Objects can still be created… sometimes

╄→гoц情女王★ 提交于 2019-11-26 06:29:54
问题 The naive, optimistic and oh.. so wrong view of the c++11 uniform initialization syntax I thought that since C++11 user-defined type objects should be constructed with the new {...} syntax instead of the old (...) syntax (except for constructor overloaded for std::initializer_list and similar parameters (e.g. std::vector : size ctor vs 1 elem init_list ctor)). The benefits are: no narrow implicit conversions, no problem with the most vexing parse, consistency(?). I saw no problem as I thought

Initializing a member array in constructor initializer

六月ゝ 毕业季﹏ 提交于 2019-11-26 03:56:32
问题 class C { public: C() : arr({1,2,3}) //doesn\'t compile {} /* C() : arr{1,2,3} //doesn\'t compile either {} */ private: int arr[3]; }; I believe the reason is that arrays can be initialized only with = syntax, that is: int arr[3] = {1,3,4}; Questions How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Does the C++03 standard say anything special about initializing aggregates (including arrays) in ctor

C++11 aggregate initialization for classes with non-static member initializers

天大地大妈咪最大 提交于 2019-11-26 03:30:34
问题 Is it allowed in standard: struct A { int a = 3; int b = 3; }; A a{0,1}; // ??? Is this class still aggregate? clang accepts this code, but gcc doesn\'t. 回答1: In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no