The table summarizes how brackets {}
and ()
are related to list-initialization in various contexts. The column Allows Narrowing Conversion indicates whether implicit type conversions that lose information are allowed. The column Allows Explicit Constructors indicates whether the syntax can call constructors marked as explicit. The columns Use for Aggregates and Use for User-Defined Types show the applicability of each initialization type for aggregates like arrays (e.g., int x[3][4]
) and structs, and user-defined types like classes, respectively.
Initialization Type | Syntax | Allows Narrowing Conversion | Allows Explicit Constructors | Use for Aggregates | Use for User-Defined Types | Remarks |
---|---|---|---|---|---|---|
Direct Initialization (()) | int a(42); | Yes | No | No | Yes | Traditional C++ syntax |
Direct-list-initialization | int a{42}; | No | Yes | Yes | Yes | C++11 onwards; type-safe |
Copy Initialization (=) | int a = 42; | Yes | No | No | Yes | Traditional C++ syntax |
Copy-list-initialization | int a = {42}; | No | No | Yes | Yes | C++11 onwards; type-safe |
Copy list initialization
(Type obj = {value1, value2, …};) is a syntactic sugar for direct list initialization
(Type obj{value1, value2, …};).