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]
123 questions
106
votes
7 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] =…
![](../../users/profiles/469935.webp)
Armen Tsirunyan
- 120,726
- 52
- 304
- 418
95
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:…
![](../../users/profiles/2069064.webp)
Barry
- 247,587
- 26
- 487
- 819
89
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.…
![](../../users/profiles/34509.webp)
Johannes Schaub - litb
- 466,055
- 116
- 851
- 1,175
55
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…
![](../../users/profiles/2805305.webp)
bolov
- 58,757
- 13
- 108
- 182
47
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
44
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.…
![](../../users/profiles/34509.webp)
Johannes Schaub - litb
- 466,055
- 116
- 851
- 1,175
34
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.
![](../../users/profiles/3120079.webp)
Bikineev
- 1,645
- 14
- 20
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…
![](../../users/profiles/835103.webp)
vkrzv
- 1,574
- 1
- 16
- 43
24
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] +…
![](../../users/profiles/2642059.webp)
Jonathan Mee
- 35,107
- 16
- 95
- 241
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}}; //…
![](../../users/profiles/11316188.webp)
iwans
- 293
- 1
- 10
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…
![](../../users/profiles/8171357.webp)
Алексей Шалашов
- 203
- 1
- 4
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…
![](../../users/profiles/1685509.webp)
user16
- 273
- 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…
![](../../users/profiles/882932.webp)
Vincent
- 50,257
- 51
- 171
- 339
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…
![](../../users/profiles/1989995.webp)
alexeykuzmin0
- 6,008
- 1
- 24
- 49
15
votes
1 answer
Initialisation of std::array<>
Consider the following code:
#include
struct A
{
int a;
int b;
};
static std::array x1 =
{
{ 1, 2 },
{ 3, 4 },
{ 5, 6 },
{ 7, 8 }
};
static std::array x2 =
{
{
{ 1, 2 },
…
![](../../users/profiles/193887.webp)
Jeremy
- 4,457
- 24
- 38