cppreference.com
T = { 1, 2, ... };
|
(1) | ||||||||
T { 1, 2, ... };
|
(2) | ( C++11) | |||||||
T = { .1 = 1 , .2 { 2 } ... };
|
(3) | ( C++20) | |||||||
T { .1 = 1 , .2 { 2 } ... };
|
(4) | ( C++20) | |||||||
1,2) .
:
- (
structunion),
|
|
( C++11) |
| ( C++11) ( C++20) | |
|
|
( C++20) |
- ( C++17)
| ( C++17) | |
| ( C++17) |
- -
| ( C++11) ( C++14) |
:
| ( C++17) | |
| ( C++17) |
:
1) :
- ,
- (
{}).
char cv[4] = {'a', 's', 'd', 'f', 0}; //
int x[] = {} //
2) :
| ( C++20) |
- , , n , n .
- (
{}) .
- , :
union u { int a; const char* b; };
u a = {1}; // OK: `a`
u b = {0, "asdf"}; // :
u c = {"asdf"}; // : int "asdf"
// C++20
u d = {.b = "asdf"}; // OK:
u e = {.a = 1, .b = "asdf"}; // :
:
struct C
{
union
{
int a;
const char* p;
};
int x;
} c = {.a = 1, .x = 3}; // c.a 1 c.x 3
|
( C++20) |
struct A
{
int x;
struct B
{
int i;
int j;
} b;
} a = {1, {2, 3}}; // a.x 1, a.b.i 2, a.b.j 3
struct base1 { int b1, b2 = 42; };
struct base2
{
base2()
{
b3 = 42;
}
int b3;
};
struct derived : base1, base2
{
int d;
};
derived d1{{1, 2}, {}, 4}; // d1.b1 1, d1.b2 2,
// d1.b3 42, d1.d 4
derived d2{{}, {}, 4}; // d2.b1 0, d2.b2 42,
// d2.b3 42, d2.d 4
, , :
| ( C++11) |
- , , .
- .
struct S
{
int a;
const char* b;
int c;
int d = b[a];
};
// ss.a 1,
// ss.b "asdf",
// ss.c int{} ( , 0),
// ss.d ss.b[ss.a] ( , 's')
S ss = {1, "asdf"};
, ,
|
( C++11) |
- ( ) .
(), , , . , - ( , ), , {}.
|
(3,4) : T, , , , T. struct A { int x; int y; int z; };
A a{.y = 2, .x = 1}; // ;
A b{.x = 1, .z = 2}; // ok, b.y 0
, , , . . union u { int a; const char* b; };
u f = {.b = "asdf"}; // OK, b
u g = {.a = 1, .b = "asdf"}; // ,
, , , , , ( , , ): struct A
{
string str;
int n = 42;
int m = -1;
};
A{.m = 21} // str {},
// n = 42
// m = 21
, , , . : , , , C, C++. struct A { int x, y; };
struct B { struct A a; };
struct A a = {.y = 1, .x = 2}; // C, C++ ( )
int arr[3] = {[1] = 5}; // C, C++ ()
struct B b = {.a.x = 0}; // C, C++ ()
struct A a = {.x = 1, 2}; // C, C++ ()
|
( C++20) |
(char, signed char, unsigned char), char8_t ( C++20), char16_t, char32_t ( C++11) wchar_t , UTF-8 ( C++20), UTF-16 , UTF-32 ( C++11) , , . , char unsigned char UTF-8, . ( C++20) ( ) . , , .
char a[] = "abc";
// char a[4] = {'a', 'b', 'c', '\0'};
// unsigned char b[3] = "abc"; // :
unsigned char b[5]{"abc"};
// unsigned char b[5] = {'a', 'b', 'c', '\0', '\0'};
wchar_t c[] = {L""}; //
// wchar_t c[6] = {L'', L'', L'', L'', L'', L'\0'};
( C++17), , , (, ).
C++11 , .
C++11 - , new .
C ; . ++.
__cpp_aggregate_bases |
201603L |
(C++17) | |
__cpp_aggregate_nsdmi |
201304L |
(C++14) | |
__cpp_aggregate_paren_init |
201902L |
(C++20) | |
__cpp_char8_t |
202207L |
(C++20) | char8_t ( (unsigned) char UTF-8)
|
__cpp_designated_initializers |
201707L |
(C++20) |
#include <array>
#include <cstdio>
#include <string>
struct S
{
int x;
struct Foo
{
int i;
int j;
int a[3];
} b;
};
int main()
{
S s1 = {1, {2, 3, {4, 5, 6}}};
S s2 = {1, 2, 3, 4, 5, 6}; // ,
S s3{1, {2, 3, {4, 5, 6}}}; // ,
//
S s4{1, 2, 3, 4, 5, 6}; // CWG 1270:
//
//
int ar[] = {1, 2, 3}; // ar int[3]
// char cr[3] = {'a', 'b', 'c', 'd'}; //
char cr[3] = {'a'}; // {'a', '\0', '\0'}
int ar2d1[2][2] = {{1, 2}, {3, 4}}; // 2D- : {1, 2}
// {3, 4}
int ar2d2[2][2] = {1, 2, 3, 4}; // : {1, 2}
// {3, 4}
int ar2d3[2][2] = {{1}, {2}}; // : {1, 0}
// {2, 0}
std::array<int, 3> std_ar2{{1, 2, 3}}; // std::array
std::array<int, 3> std_ar1 = {1, 2, 3}; //
// int ai[] = {1, 2.0}; // double int:
// C++11, C++03
std::string ars[] = {std::string(""), //
"", // ,
//
{'', '', ''}}; //
union U
{
int a;
const char* b;
};
U u1 = {1}; // OK,
// U u2 = {0, "asdf"}; // :
// U u3 = {"asdf"}; // : int
[](...) { std::puts(" ... ."); }
(
s1, s2, s3, s4, ar, cr, ar2d1, ar2d2, ar2d3, std_ar2, std_ar1, u1
);
}
//
struct base1 { int b1, b2 = 42; };
//
struct base2
{
base2() : b3(42) {}
int b3;
};
// C++17
struct derived : base1, base2 { int d; };
derived d1{{1, 2}, {}, 4}; // d1.b1 = 1, d1.b2 = 2, d1.b3 = 42, d1.d = 4
derived d2{{}, {}, 4}; // d2.b1 = 0, d2.b2 = 42, d2.b3 = 42, d2.d = 4
:
... .
C++:
| CWG 413 | C++98 | |
|
| CWG 737 | C++98 | , , , '\0'
|
|
| CWG 1270 | C++11 | |
|
| CWG 1518 | C++11 | , , |
|
| CWG 1622 | C++98 | {}
|
|
| CWG 2272 | C++98 | , , |
|
| WG | C++20 | UTF-8 charunsigned char, C C++17
|