direct initialization
From cppreference.com
Initializes an object from explicit set of constructor arguments.
Contents |
[edit] Syntax
T object ( arg );
T object |
(1) | ||||||||
T object { arg };
T object |
(2) | (since C++11) | |||||||
T ( other )
T |
(3) | ||||||||
static_cast< T >( other )
|
(4) | ||||||||
new T( args, ...)
|
(5) | ||||||||
Class:: Class() : member( args, ...) {...
|
(6) | ||||||||
[ arg](){...
|
(7) | (since C++11) | |||||||
[edit] Explanation
Direct initialization is performed in the following situations:
1) initialization with a nonempty parenthesized list of expressions
2) during list-initialization sequence, if no initializer-list constructors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
3) initialization of a prvalue temporary by functional cast or with a parenthesized expression list
4) initialization of a prvalue temporary by a static_cast expession
5) initialization of an object with dynamic storage duration by a new-expression with a non-empty initializer
6) initialization of a base or a non-static member by constructor initializer list
7) initialization of closure object members from the variables caught by copy in a lambda-expression
The effects of direct initialization are:
- If
T
is a class type, the constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
- Otherwise, if
T
is a non-class type, standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version ofT
.
[edit] Notes
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
[edit] Example
Run this code
#include <string> #include <iostream> #include <memory> struct Foo { int mem; explicit Foo(int n) : mem(n) {} }; int main() { std::string s1("test"); // constructor from const char* std::string s2(10, 'a'); std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed // std::unique_ptr<int> p = new int(1); // error: constructor is explicit Foo f(2); // f is direct-initialized: // constructor parameter n is copy-initialized from the rvalue 2 // f.mem is direct-initialized from the parameter n // Foo f2 = 2; // error: constructor is explicit std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem << '\n'; }
Output:
test aaaaaaaaaa 1 2