Aggregate initialization is a feature of C++ that allows the initialization of arrays and aggregate types using a curly brace syntax.
Questions tagged [aggregate-initialization]
162 questions
123
votes
8 answers
Initializing a member array in constructor initializer
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] =…

Armen Tsirunyan
- 130,161
- 59
- 324
- 434
97
votes
3 answers
When is a private constructor not a private constructor?
Let's say I have a type and I want to make its default constructor private. I write the following:
class C {
C() = default;
};
int main() {
C c; // error: C::C() is private within this context (g++)
// error:…

Barry
- 286,269
- 29
- 621
- 977
93
votes
8 answers
Narrowing conversions in C++0x. Is it just me, or does this sound like a breaking change?
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.…

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
56
votes
3 answers
Deleted default constructor. Objects can still be created... sometimes
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…

bolov
- 72,283
- 15
- 145
- 224
48
votes
2 answers
Brace elision in std::array initialization
Suppose there's an std::array to be initialized. It's okay if using double braces:
std::array x = {{0, 1}};
std::array x{{0, 1}};
It's also okay to use single braces in the good old aggregate initialization, as the brace elision…
user784668
47
votes
5 answers
Is it possible to prevent omission of aggregate initialization members?
I have a struct with many members of the same type, like this
struct VariablePointers {
VariablePtr active;
VariablePtr wasactive;
VariablePtr filename;
};
The problem is that if I forget to initialize one of the struct members (e.g.…

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
35
votes
1 answer
C++11 aggregate initialization for classes with non-static member initializers
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.

Bikineev
- 1,685
- 15
- 20
31
votes
4 answers
How I can keep aggregate initialization while also adding custom constructors?
If I don't define a constructor in a struct, I can initialize it by just picking a certain value like this:
struct Foo {
int x, y;
};
Foo foo = {.y = 1};
But if I add new default constructor then I lose this feature:
struct Bar {
int x,…

ellipticaldoor
- 1,122
- 11
- 24
29
votes
1 answer
Can I Reference Previous Members of an Initializer List?
Say I want to refer to a member of an initializer_list that I already defined. Can I do it?
This code compiles and gives the expected: "13 55 " in both Visual Studio and gcc, I'd just like to know that it's legal:
const int foo[2] = {13, foo[0] +…

Jonathan Mee
- 37,899
- 23
- 129
- 288
26
votes
1 answer
Visual Studio 2019 does not handle aggregate initialization of dynamic array of structs correctly
The code below prints garbage (or zeroes) if compiled with VC++ 2017 and "1122" if compiled with GCC or Clang (https://rextester.com/JEV81255). Is it bug of VC++ or I'm missing something here?
#include
struct Item {
int id;
int…

user10101
- 1,704
- 2
- 20
- 49
21
votes
2 answers
Why can't std::array, 3> be initialized using nested initializer lists, but std::vector> can?
See this example: https://godbolt.org/z/5PqYWP
How come this array of pairs can't be initialized in the same way as a vector of pairs?
#include
#include
int main()
{
std::vector> v{{1,2},{3,4},{5,6}}; //…

iwans
- 445
- 3
- 13
19
votes
2 answers
Default value of function parameter initialized by list initialization
Could anyone help me with the following problem?
There is a simple code:
#include
struct A {
std::vector vec;
};
void func (A &&a = {}) {}
int main()
{
func();
return 0;
}
When I try to compile it by gcc 5.4.0 I get the…

Алексей Шалашов
- 203
- 1
- 6
18
votes
2 answers
Initializing a struct with aggregate initialization and member initializers
Consider the following example:
#include
#include
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…

user16
- 283
- 2
- 9
17
votes
2 answers
Build tuple from heterogeneous initializer list at function call
Consider the following function
template
void f(std::tuple t, std::tuple u)
{
std::cout << sizeof...(T) << " " << sizeof...(U) << std::endl;
}
int main(int argc, char* argv[])
{
f({3, 3.5, "Hello…

Vincent
- 57,703
- 61
- 205
- 388
16
votes
3 answers
What are the rules of field-by-field constructor generation?
I have found that the possibility of usage of initializer list syntax for a class depends on whether or not the class fields have default values. Why?
To be precise, consider the following code:
class S
{
public:
int a;
};
...
int a;
S…

alexeykuzmin0
- 6,344
- 2
- 28
- 51