General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
Initializes an object from another object.
Contents |
Tobject= other; | (1) | ||||||||
Tobject= { other}; | (2) | (until C++11) | |||||||
f( other) | (3) | ||||||||
return other; | (4) | ||||||||
throw object;
| (5) | ||||||||
Tarray[ N] = { other-sequence}; | (6) | ||||||||
Copy-initialization is performed in the following situations:
T
is declared with the initializer consisting of an equals sign followed by an expression.T
is declared with the initializer consisting of an equals sign followed by a brace-enclosed expression (Note: as of C++11, this is classified aslist initialization, and narrowing conversion is not allowed).The effects of copy-initialization are:
| (since C++17) |
T
is a class type and the cv-unqualified version of the type ofother isT
or a class derived fromT
, thenon-explicit constructors ofT
are examined and the best match is selected by overload resolution. That constructor is then called to initialize the object.T
is a class type, and the cv-unqualified version of the type ofother is notT
or derived fromT
, or ifT
is non-class type, but the type ofother is a class type,user-defined conversion sequences that can convert from the type ofother toT
(or to a type derived fromT
ifT
is a class type and a conversion function is available) are examined and the best one is selected through overload resolution. The result of the conversion, which is arvalue temporary(until C++11)prvalue temporary(since C++11)(until C++17)prvalue expression(since C++17) of the cv-unqualified version ofT
if aconverting constructor was used, is then used todirect-initialize the object.The last step is usuallyoptimized out and the result of the conversion is constructed directly in the memory allocated for the target object, but the appropriate constructor (move or copy) is required to be accessible even though it's not used.(until C++17)T
nor the type ofother are class types),standard conversions are used, if necessary, to convert the value ofother to the cv-unqualified version ofT
.Copy-initialization is less permissive than direct-initialization:explicit constructors are notconverting constructors and are not considered for copy-initialization.
struct Exp{explicit Exp(constchar*){}};// not convertible from const char*Exp e1("abc");// OKExp e2="abc";// Error, copy-initialization does not consider explicit constructor struct Imp{ Imp(constchar*){}};// convertible from const char*Imp i1("abc");// OKImp i2="abc";// OK
In addition, the implicit conversion in copy-initialization must produceT
directly from the initializer, while, e.g. direct-initialization expects an implicit conversion from the initializer to an argument ofT
's constructor.
struct S{ S(std::string){}};// implicitly convertible from std::stringS s("abc");// OK: conversion from const char[4] to std::stringS s="abc";// Error: no conversion from const char[4] to SS s="abc"s;// OK: conversion from std::string to S
Ifother is an rvalue expression, amove constructor will be selected by overload resolution and called during copy-initialization. This is still considered copy-initialization; there is no special term (e.g., move-initialization) for this case.
Implicit conversion is defined in terms of copy-initialization: if an object of typeT
can be copy-initialized with expressionE
, thenE
is implicitly convertible toT
.
The equals sign,=
, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.
#include <memory>#include <string>#include <utility> struct A{ operatorint(){return12;}}; struct B{ B(int){}}; int main(){std::string s="test";// OK: constructor is non-explicitstd::string s2= std::move(s);// this copy-initialization performs a move // std::unique_ptr<int> p = new int(1); // error: constructor is explicitstd::unique_ptr<int> p(newint(1));// OK: direct-initialization int n=3.14;// floating-integral conversionconstint b= n;// const doesn't matterint c= b;// ...either way A a; B b0=12;// B b1 = a; // < error: conversion from 'A' to non-scalar type 'B' requested B b2{a};// < identical, calling A::operator int(), then B::B(int) B b3={a};// <auto b4= B{a};// < // b0 = a; // < error, assignment operator overload needed [](...){}(c, b0, b3, b4);// pretend these variables are used}
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 5 | C++98 | the cv-qualification of the destination type is applied to the temporary initialized by a converting constructor | the temporary is not cv-qualified |
CWG 177 | C++98 | the value category of the temporary created during copy-initialization of a class object is unspecified | specified as rvalue |