Namespaces
Variants
Views
Actions

direct initialization

From cppreference.com
< cpp‎ | language
Revision as of 22:28, 2 November 2012 by P12bot (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Initializes an object from explicit set of constructor arguments.

Contents

Syntax

Template:sparam Template:sparam ( Template:sparam );

Template:sparam Template:sparam ( Template:sparam );

(1)
Template:sparam Template:sparam { Template:sparam };

Template:sparam Template:sparam { Template:sparam };

(2) (since C++11)
Template:sparam ( Template:sparam )

Template:sparam ( Template:sparam );

(3)
static_cast< Template:sparam >( Template:sparam ) (4)
new Template:sparam(Template:sparam) (5)
Template:sparam::Template:sparam() : Template:sparam(Template:sparam) {... (6)
[arg](){... (7) (since C++11)

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 constuctors 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 of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.

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.

Example

#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

See also