aggregate-initialization

Nested aggregate initialization of std::array [duplicate]

跟風遠走 提交于 2019-12-10 03:49:14
问题 This question already has an answer here : When can outer braces be omitted in an initializer list? (1 answer) Closed 2 years ago . I wonder, why declaration of std_arr in the following code generates an error, while c_arr compiles well: struct S { int a, b; }; S c_arr[] = {{1, 2}, {3, 4}}; // OK std::array<S, 2> std_arr = {{1, 2}, {3, 4}}; // Error: too many initializers Both std::array and S are aggregates. From aggregate initialization on cppreference.com: If the initializer clause is a

Initializing a struct with aggregate initialization and member initializers [duplicate]

。_饼干妹妹 提交于 2019-12-09 08:39:17
问题 This question already has an answer here : C++11 aggregate initialization for classes with non-static member initializers (1 answer) Closed last year . Consider the following example: #include <iostream> #include <string> struct ABC { std::string str; unsigned int id ;/* = 0 : error: no matching constructor for initialization of 'ABC'*/ }; int main() { ABC abc{"hi", 0}; std::cout << abc.str << " " << abc.id << std::endl; return 0; } When defining the structure ABC without default value for id

How to avoid {} when using aggregate initialization with empty base class

萝らか妹 提交于 2019-12-08 17:38:07
问题 C++17's aggregate initialization for base class is awesome, but it is verbose when the base is only there to provide some functions (so no data members). Here is minimal example: #include <cstddef> struct base_pod { // functions like friend compare operator }; template<typename T, std::size_t N> struct der_pod : public base_pod { T k[N]; }; int main() { der_pod<int, 2> dp {{}, {3, 3} }; } As the example above shows, I have to provide empty {} , otherwise compile error will occur. live demo.

Aggregate Initialization Safety in C++

断了今生、忘了曾经 提交于 2019-12-08 16:39:27
问题 Suppose I have the following struct: struct sampleData { int x; int y; }; And when used, I want to initialize variables of sampleData type to a known state. sampleData sample = { 1, 2 } Later, I decide that I need additional data stored in my sampleData struct, as follows: struct sampleData { int x; int y; int z; }; It is my understanding that the two field initialization left over from my pre- z data structure is still a valid statement, and will be compiled., populating the missing fields

Initializing array through explicit constructor

佐手、 提交于 2019-12-08 11:40:48
问题 I'm writing a class that has an explicit constructor taking a const char* argument. For the intents and purposes of this question it looks like this: struct Symbol { Symbol()=default; explicit Symbol(const char*); }; Now I want to write an example for documentation purposes that initializes an array (array/vector/list - I don't care about the exact type) and I need the example to be as clear and concise as possible. Ideally it would look like this: Symbol symbols[] = { "a", "b", "c"}; That

Is Aggregate Initialization of Bit-Fields Allowed?

五迷三道 提交于 2019-12-05 03:04:15
问题 I have a struct which contains bit-fields: struct Foo { unsigned a : 16, b : 16; }; And I want to know if I can use aggregate initialization on it's bit-fields. For example: struct Foo bar = {13, 42}; I note that this does work in gcc 5.1 and Visual Studio 2015. I'd just like something to certify this was standard approved initialization for both C and C++. 回答1: From C++14 [dcl.init.aggr] we have An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no

Why can I initialize a regular array from {}, but not a std::array

懵懂的女人 提交于 2019-12-05 03:01:55
This works: int arr[10] = {}; All elements of arr are value-initialized to zero. Why doesn't this work: std::array<int, 10> arr({}); I get the following warning from g++ (version 4.8.2): warning: missing initializer for member ‘std::array<int, 10ul>::_M_elems’ There are two issues one which is a matter of style and the warning. Although it may not be obvious, aggregate initialization is happening on a temporary which is then being used as an argument to the copy constructor. The more idiomatic to do this initialization would be as follows: std::array<int, 10> arr = {}; Although this still

Initialisation of std::array<>

最后都变了- 提交于 2019-12-04 08:24:29
问题 Consider the following code: #include <array> struct A { int a; int b; }; static std::array<A, 4> x1 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; static std::array<A, 4> x2 = { { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } } }; static std::array<A, 4> x3 = { A{ 1, 2 }, A{ 3, 4 }, A{ 5, 6 }, A{ 7, 8 } }; static std::array<A, 4> x4 = { A{ 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; Compiling with gcc: $ gcc -c --std=c++11 array.cpp array.cpp:15:1: error: too many initializers for ‘std::array<A, 4ul>’ }

Is Aggregate Initialization of Bit-Fields Allowed?

喜欢而已 提交于 2019-12-03 18:13:15
I have a struct which contains bit-fields: struct Foo { unsigned a : 16, b : 16; }; And I want to know if I can use aggregate initialization on it's bit-fields. For example: struct Foo bar = {13, 42}; I note that this does work in gcc 5.1 and Visual Studio 2015. I'd just like something to certify this was standard approved initialization for both C and C++. From C++14 [dcl.init.aggr] we have An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions

Initializing a struct with aggregate initialization and member initializers [duplicate]

梦想的初衷 提交于 2019-12-03 10:19:31
This question already has an answer here: C++11 aggregate initialization for classes with non-static member initializers 1 answer Consider the following example: #include <iostream> #include <string> struct ABC { std::string str; unsigned int id ;/* = 0 : error: no matching constructor for initialization of 'ABC'*/ }; int main() { ABC abc{"hi", 0}; std::cout << abc.str << " " << abc.id << std::endl; return 0; } When defining the structure ABC without default value for id clang 3.x and gcc 4.8.x compile the code without problems. However, after adding a default argument for "id" I get the