%!TEX root = std.tex
\rSec0[special]{Special member functions}

%gram: \rSec1[gram.special]{Special member functions}
%gram:

\indextext{special~member~function|see{constructor, destructor, inline function,
user-defined conversion, virtual function}}%
\indextext{\idxcode{X(X\&)}|see{copy~constructor}}%
\indextext{\~@\tcode{\tilde}|see{destructor}}%
\indextext{assignment!copy|see{assignment operator, copy}}%
\indextext{assignment!move|see{assignment operator, move}}%
\indextext{implicitly-declared~default~constructor|see{constructor, default}}

\pnum
\indextext{constructor!default}%
\indextext{constructor!copy}%
\indextext{constructor!move}%
\indextext{assignment operator!copy}%
\indextext{assignment operator!move}%
The default constructor~(\ref{class.ctor}),
copy constructor and copy assignment operator~(\ref{class.copy}),
move constructor and move assignment operator~(\ref{class.copy}),
and destructor~(\ref{class.dtor}) are
\term{special member functions}.
\enternote The implementation will implicitly declare these member functions for some class
types when the program does not explicitly declare them.
The implementation will implicitly define them if they are odr-used~(\ref{basic.def.odr}).
See~\ref{class.ctor}, \ref{class.dtor} and~\ref{class.copy}. \exitnote
An implicitly-declared special member function is declared at the closing
\tcode{\}} of the \grammarterm{class-specifier}.
Programs shall not define implicitly-declared special member functions.

\pnum
Programs may explicitly refer to implicitly-declared special member functions.
\enterexample
a program may explicitly call, take the address of or form a pointer to member
to an implicitly-declared special member function.

\begin{codeblock}
struct A { };                   // implicitly declared \tcode{A::operator=}
struct B : A {
  B& operator=(const B &);
};
B& B::operator=(const B& s) {
  this->A::operator=(s);        // well formed
  return *this;
}
\end{codeblock}
\exitexample

\pnum
\enternote
The special member functions affect the way objects of class type are created,
copied, moved, and destroyed, and how values can be converted to values of other types.
Often such special member functions are called implicitly.
\exitnote

\pnum
\indextext{access~control!member~function~and}%
Special member functions obey the usual access rules (Clause~\ref{class.access}).
\enterexample
declaring a constructor
\tcode{protected}
ensures that only derived classes and friends can create objects using it.
\exitexample

\pnum
For a class, its non-static data members, its non-virtual direct base classes,
and, if the class is not abstract~(\ref{class.abstract}), its virtual base
classes are called its \term{potentially constructed subobjects}.

\rSec1[class.ctor]{Constructors}%
\indextext{constructor}

\pnum
Constructors do not have names.
A declaration of a constructor uses a function declarator~(\ref{dcl.fct}) of the form

\begin{ncbnf}
ptr-declarator \terminal{(} parameter-declaration-clause \terminal{)} exception-specification\opt attribute-specifier-seq\opt
\end{ncbnf}

where the \grammarterm{ptr-declarator} consists solely of an
\grammarterm{id-expression}, an optional \grammarterm{attribute-specifier-seq},
and optional surrounding parentheses, and the \grammarterm{id-expression} has
one of the following forms:

\begin{itemize}
\item
in a \grammarterm{member-declaration} that belongs to the
\grammarterm{member-specification} of a class but is not a friend
declaration~(\ref{class.friend}), the \grammarterm{id-expression} is the
injected-class-name (Clause~\ref{class}) of the immediately-enclosing class;

\item
in a \grammarterm{member-declaration} that belongs to the
\grammarterm{member-specification} of a class template but is not a friend
declaration, the \grammarterm{id-expression} is a \grammarterm{class-name} that
names the current instantiation~(\ref{temp.dep.type}) of the
immediately-enclosing class template; or

\item
in a declaration at namespace scope or in a friend declaration, the
\grammarterm{id-expression} is a \grammarterm{qualified-id} that names a
constructor~(\ref{class.qual}).
\end{itemize}

The \grammarterm{class-name} shall not be a \grammarterm{typedef-name}. In a constructor
declaration, each \grammarterm{decl-specifier} in the optional
\grammarterm{decl-specifier-seq} shall be \tcode{friend}, \tcode{inline},
\tcode{explicit}, or \tcode{constexpr}.
\enterexample

\begin{codeblock}
struct S {
  S();              // declares the constructor
};

S::S() { }          // defines the constructor
\end{codeblock}
\exitexample

\pnum
A constructor is used to initialize objects of its class type.
Because constructors do not have names, they are never found during
name lookup; however an explicit type conversion using the functional
notation~(\ref{expr.type.conv}) will cause a constructor to be called to
initialize an object.
\enternote
For initialization of objects of class type see~\ref{class.init}.
\exitnote

\pnum
\indextext{\idxcode{const}!constructor~and}%
\indextext{\idxcode{volatile}!constructor~and}%
A constructor can be invoked for a
\tcode{const},
\tcode{volatile}
or
\tcode{const}
\tcode{volatile}
object.
\indextext{restriction!constructor}%
\tcode{const}
and
\tcode{volatile}
semantics~(\ref{dcl.type.cv}) are not applied on an object under construction.
They come into effect when the constructor for the
most derived object~(\ref{intro.object}) ends.

\pnum
\indextext{constructor!inheritance~of}%
\indextext{constructor!default}%
\indextext{constructor!non-trivial}%
A
\term{default}
constructor for a class
\tcode{X}
is a constructor of class
\tcode{X}
that can be called without an argument.
\indextext{implicitly-declared~default~constructor}%
If there is no user-declared constructor for class
\tcode{X},
a constructor having no parameters is implicitly declared
as defaulted~(\ref{dcl.fct.def}).
An implicitly-declared default constructor is an
\tcode{inline}
\tcode{public}
member of its class.
A defaulted default constructor for class \tcode{X} is defined as deleted if:

\begin{itemize}
\item \tcode{X} is a union-like class that has a variant
  member with a non-trivial default constructor,

\item any non-static data member with no \grammarterm{brace-or-equal-initializer} is of reference type,

\item any non-variant non-static data member of const-qualified type (or array
thereof) with no \grammarterm{brace-or-equal-initializer} does not have a user-provided default constructor,

\item \tcode{X} is a union and all of its variant members are of const-qualified
type (or array thereof),

\item \tcode{X} is a non-union class and all members of any anonymous union member are
of const-qualified type (or array thereof),

\item any potentially constructed subobject, except for a non-static data member
with a \grammarterm{brace-or-equal-initializer}, has
class type \tcode{M} (or array thereof) and either \tcode{M}
has no default constructor or overload resolution
(\ref{over.match}) as applied to \tcode{M}'s default
constructor results in an ambiguity or in a function that is deleted or
inaccessible from the defaulted default constructor, or

\item any potentially constructed subobject has a type
with a destructor that is deleted or inaccessible from the defaulted default
constructor.
\end{itemize}

A default constructor is
trivial
if it is not user-provided and if:

\begin{itemize}
\item
its class has no virtual functions~(\ref{class.virtual}) and no virtual base
classes~(\ref{class.mi}), and

\item no non-static data member of its class has a \grammarterm{brace-or-equal-initializer}, and

\item
all the direct base classes of its class have trivial default constructors, and

\item
for all the non-static data members of its class that are of class
type (or array thereof), each such class has a trivial default constructor.
\end{itemize}

Otherwise, the default constructor is
\grammarterm{non-trivial}.

\pnum
\indextext{constructor!implicitly defined}%
A default constructor
that is defaulted and not defined as deleted
is
\term{implicitly defined}
when it is odr-used~(\ref{basic.def.odr})
to create an object of its class type~(\ref{intro.object})
or when it is explicitly defaulted after its first declaration.
The implicitly-defined default constructor performs the set of
initializations of the class that would be performed by a user-written default
constructor for that class with no
\grammarterm{ctor-initializer}~(\ref{class.base.init}) and an empty
\grammarterm{compound-statement}.
If that user-written default constructor would be ill-formed,
the program is ill-formed.
If that user-written default constructor would satisfy the requirements
of a \tcode{constexpr} constructor~(\ref{dcl.constexpr}), the implicitly-defined
default constructor is \tcode{constexpr}.
Before the defaulted default constructor for a class is
implicitly defined,
all the non-user-provided default constructors for its base classes and
its non-static data members shall have been implicitly defined.
\enternote
An implicitly-declared default constructor has an
\grammarterm{exception-specification}~(\ref{except.spec}).
An explicitly-defaulted definition might have an
implicit \grammarterm{exception-specification,} see~\ref{dcl.fct.def}.
\exitnote

\pnum
\indextext{constructor!implicitly called}%
Default constructors are called implicitly to create class objects of static, thread,
or automatic storage duration~(\ref{basic.stc.static}, \ref{basic.stc.thread}, \ref{basic.stc.auto}) defined
without an initializer~(\ref{dcl.init}),
are called to create class objects of dynamic storage duration~(\ref{basic.stc.dynamic}) created by a
\grammarterm{new-expression}
in which the
\grammarterm{new-initializer}
is omitted~(\ref{expr.new}), or
are called when the explicit type conversion syntax~(\ref{expr.type.conv}) is
used.
A program is ill-formed if the default constructor for an object
is implicitly used and the constructor is not accessible (Clause~\ref{class.access}).

\pnum
\enternote
\indextext{order~of~execution!base~class constructor}%
\indextext{order~of~execution!member constructor}%
\ref{class.base.init} describes the order in which constructors for base
classes and non-static data members are called and
describes how arguments can be specified for the calls to these constructors.
\exitnote

\pnum
\indextext{restriction!constructor}%
A
\tcode{return}
statement in the body of a constructor shall not specify a return value.
\indextext{constructor!address~of}%
The address of a constructor shall not be taken.

\pnum
\indextext{object!unnamed}%
\indextext{constructor!explicit call}%
A functional notation type conversion~(\ref{expr.type.conv}) can be used
to create new objects of its type.
\enternote
The syntax looks like an explicit call of the constructor.
\exitnote
\enterexample
\indextext{example!constructor}%

\begin{codeblock}
complex zz = complex(1,2.3);
cprint( complex(7.8,1.2) );
\end{codeblock}
\exitexample

\pnum
An object created in this way is unnamed.
\enternote
\ref{class.temporary} describes the lifetime of temporary objects.
\exitnote
\enternote
Explicit constructor calls do not yield lvalues, see~\ref{basic.lval}.
\exitnote

\pnum
\enternote
\indextext{member function!constructor~and}%
some language constructs have special semantics when used during construction;
see~\ref{class.base.init} and~\ref{class.cdtor}.
\exitnote

\pnum
During the construction of a
\tcode{const}
object, if the value of the object or any of its subobjects is
accessed through a glvalue that is not obtained, directly or indirectly, from
the constructor's
\tcode{this}
pointer, the value of the object or subobject thus obtained is unspecified.
\enterexample

\begin{codeblock}
struct C;
void no_opt(C*);

struct C {
  int c;
  C() : c(0) { no_opt(this); }
};

const C cobj;

void no_opt(C* cptr) {
  int i = cobj.c * 100;         // value of \tcode{cobj.c} is unspecified
  cptr->c = 1;
  cout << cobj.c * 100          // value of \tcode{cobj.c} is unspecified
       << '\n';
}
\end{codeblock}
\exitexample

\rSec1[class.temporary]{Temporary objects}

\pnum
\indextext{object~temporary|see{temporary}}%
\indextext{temporary}%
\indextext{optimization~of~temporary|see{elimination~of~temporary}}%
\indextext{temporary!elimination~of}%
\indextext{temporary!implementation-defined~generation~of}%
Temporaries of class type are created in various contexts:
binding a reference to a prvalue~(\ref{dcl.init.ref}),
returning a prvalue~(\ref{stmt.return}),
a conversion that creates a prvalue~(\ref{conv.lval}, \ref{expr.static.cast},
\ref{expr.const.cast}, \ref{expr.cast}),
throwing an exception~(\ref{except.throw}), and in some initializations~(\ref{dcl.init}).
\enternote
The lifetime of exception objects is described in~\ref{except.throw}.
\exitnote
Even when the creation of the temporary object is
unevaluated (Clause~\ref{expr}) or otherwise
avoided~(\ref{class.copy}),
all the semantic restrictions shall be respected as if the temporary object
had been created and later destroyed.
\enternote
This includes accessibility~(\ref{class.access}) and whether it is deleted,
for the constructor selected and for the destructor. However, in the special
case of a function call used as the operand of a
\grammarterm{decltype-specifier}~(\ref{expr.call}), no temporary is introduced,
so the foregoing does not apply to the prvalue of any such function call.
\exitnote

\pnum
\enterexample Consider the following code:
\begin{codeblock}
class X {
public:
  X(int);
  X(const X&);
  X& operator=(const X&);
  ~X();
};

class Y {
public:
  Y(int);
  Y(Y&&);
  ~Y();
};

X f(X);
Y g(Y);

void h() {
  X a(1);
  X b = f(X(2));
  Y c = g(Y(3));
  a = f(a);
}
\end{codeblock}

\indextext{class~object~copy|see{copy~constructor}}%
\indextext{constructor!copy}%
An implementation might use a temporary in which to construct
\tcode{X(2)}
before passing it to
\tcode{f()}
using
\tcode{X}'s
copy constructor; alternatively,
\tcode{X(2)}
might be constructed in the space used to hold the argument.
Likewise, an implementation might use a temporary in which to construct
\tcode{Y(3)} before passing it to \tcode{g()} using \tcode{Y}'s move constructor;
alternatively, \tcode{Y(3)} might be constructed in the space used to hold the argument.
Also, a temporary might be used to hold the result of
\tcode{f(X(2))}
before copying it to
\tcode{b}
using
\tcode{X}'s
copy constructor; alternatively,
\tcode{f()}'s
result might be constructed in
\tcode{b}.
Likewise, a temporary might be used to hold the result of \tcode{g(Y(3))} before
moving it to \tcode{c} using \tcode{Y}'s move constructor; alternatively, \tcode{g()}'s
result might be constructed in \tcode{c}.
On the other hand, the expression
\tcode{a=f(a)}
requires a temporary for
the result of \tcode{f(a)}, which is then assigned to \tcode{a}.
\exitexample

\pnum
\indextext{temporary!constructor~for}%
\indextext{temporary!destructor~for}%
\indextext{temporary!destruction~of}%
When an implementation introduces a temporary object of a class that has a
non-trivial constructor~(\ref{class.ctor}, \ref{class.copy}), it shall ensure that
a constructor is called for the temporary object.
Similarly, the destructor shall be called for a temporary with a non-trivial
destructor~(\ref{class.dtor}).
Temporary objects are destroyed as the last step
in evaluating
the full-expression~(\ref{intro.execution})
that (lexically) contains the point where
they were created.
This is true even if that evaluation ends in throwing an exception.
The
\indextext{value computation}%
value computations and
\indextext{side effects}%
side effects of destroying a temporary object
are associated only with the full-expression, not with any specific
subexpression.

\pnum
\indextext{initializer!temporary~and declarator}%
\indextext{temporary!order~of destruction~of}%
There are two contexts in which temporaries are destroyed at a different
point than the end of the full-expression.
The first context is when a default constructor is called to initialize an
element of an array. If the constructor has one or more default arguments,
the destruction of every temporary created in
a default argument is sequenced before the construction of the next array element, if any.

\pnum
The second context is when a reference is bound to a
temporary.\footnote{The same rules apply to initialization of an
  \tcode{initializer_list} object~(\ref{dcl.init.list}) with its
  underlying temporary array}
The temporary to which the reference is bound or the temporary
that is the complete object of a subobject to which the reference is bound
persists for the lifetime of the reference except:

\begin{itemize}
\item A temporary bound to a reference member in a constructor's \grammarterm{ctor-initializer}~(\ref{class.base.init}) persists until the constructor exits.

\item A temporary bound to a reference parameter in a function call~(\ref{expr.call})
persists until the completion of the full-expression containing the call.

\item The lifetime of a temporary bound to the returned value in a function return statement~(\ref{stmt.return}) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.

\item A temporary bound to a reference in a \grammarterm{new-initializer}~(\ref{expr.new}) persists until the completion of the full-expression containing the \grammarterm{new-initializer}. \enterexample
\begin{codeblock}
struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} };   // Creates dangling reference
\end{codeblock}
\exitexample \enternote This may introduce a dangling reference, and implementations are encouraged to issue a warning in such a case. \exitnote
\end{itemize}

The destruction of a temporary whose lifetime is not extended by being
bound to a reference is sequenced before the destruction of every
temporary which is constructed earlier in the same full-expression.
If the lifetime of two or more temporaries to which references are bound ends
at the same point,
these temporaries are destroyed at that point in the reverse order of the
completion of their construction.
In addition, the destruction of temporaries bound to references shall
take into account the ordering of destruction of objects with static, thread, or
automatic storage duration~(\ref{basic.stc.static}, \ref{basic.stc.thread}, \ref{basic.stc.auto});
that is, if
\tcode{obj1}
is an object with the same storage duration as the temporary and
created before the temporary is created 
the temporary shall be destroyed before
\tcode{obj1}
is destroyed;
if
\tcode{obj2}
is an object with the same storage duration as the temporary and
created after the temporary is created
the temporary shall be destroyed after
\tcode{obj2}
is destroyed.
\enterexample

\begin{codeblock}
struct S {
  S();
  S(int);
  friend S operator+(const S&, const S&);
  ~S();
};
S obj1;
const S& cr = S(16)+S(23);
S obj2;
\end{codeblock}

the expression
\tcode{S(16) + S(23)}
creates three temporaries:
a first temporary
\tcode{T1}
to hold the result of the expression
\tcode{S(16)},
a second temporary
\tcode{T2}
to hold the result of the expression
\tcode{S(23)},
and a third temporary
\tcode{T3}
to hold the result of the addition of these two expressions.
The temporary
\tcode{T3}
is then bound to the reference
\tcode{cr}.
It is unspecified whether
\tcode{T1}
or
\tcode{T2}
is created first.
On an implementation where
\tcode{T1}
is created before
\tcode{T2},
\tcode{T2}
shall be destroyed before
\tcode{T1}.
The temporaries
\tcode{T1}
and
\tcode{T2}
are bound to the reference parameters of
\tcode{operator+};
these temporaries are destroyed at the end of the full-expression
containing the call to
\tcode{operator+}.
The temporary
\tcode{T3}
bound to the reference
\tcode{cr}
is destroyed at the end of
\tcode{cr}'s
lifetime, that is, at the end of the program.
In addition, the order in which
\tcode{T3}
is destroyed takes into account the destruction order of other objects with
static storage duration.
That is, because
\tcode{obj1}
is constructed before
\tcode{T3},
and
\tcode{T3}
is constructed before
\tcode{obj2},
\tcode{obj2}
shall be destroyed before
\tcode{T3},
and
\tcode{T3}
shall be destroyed before
\tcode{obj1}.
\exitexample

\rSec1[class.conv]{Conversions}

\pnum
\indextext{conversion!class}%
\indextext{conversion!user-defined}%
\indextext{constructor, conversion by|see{conversion, user-defined}}%
\indextext{conversion~function|see{conversion, user-defined}}%
\indextext{conversion!implicit}%
Type conversions of class objects can be specified by constructors and
by conversion functions.
These conversions are called
\term{user-defined conversions}
and are used for implicit type conversions (Clause~\ref{conv}),
for initialization~(\ref{dcl.init}),
and for explicit type conversions~(\ref{expr.cast}, \ref{expr.static.cast}).

\pnum
User-defined conversions are applied only where they are unambiguous~(\ref{class.member.lookup}, \ref{class.conv.fct}).
Conversions obey the access control rules (Clause~\ref{class.access}).
Access control is applied after ambiguity resolution~(\ref{basic.lookup}).

\pnum
\enternote
See~\ref{over.match} for a discussion of the use of conversions in function calls
as well as examples below.
\exitnote

\pnum
\indextext{conversion!implicit user-defined}%
At most one user-defined conversion (constructor or conversion function)
is implicitly applied to a single value.

\enterexample

\begin{codeblock}
struct X {
  operator int();
};

struct Y {
  operator X();
};

Y a;
int b = a;          // error
                    // \tcode{a.operator X().operator int()} not tried
int c = X(a);       // OK: \tcode{a.operator X().operator int()}
\end{codeblock}
\exitexample

\pnum
User-defined conversions are used implicitly only if they are unambiguous.
\indextext{name~hiding!user-defined conversion~and}%
A conversion function in a derived class does not hide a conversion function
in a base class unless the two functions convert to the same type.
Function overload resolution~(\ref{over.match.best}) selects the best
conversion function to perform the conversion.
\enterexample

\begin{codeblock}
struct X {
  operator int();
};

struct Y : X {
    operator char();
};

void f(Y& a) {
  if (a) {          // ill-formed:
                    // \tcode{X::operator int()} or \tcode{Y::operator char()}
  }
}
\end{codeblock}
\exitexample

\rSec2[class.conv.ctor]{Conversion by constructor}%
\indextext{conversion!user-defined}

\pnum
A constructor declared without the
\grammarterm{function-specifier}
\tcode{explicit}
specifies a conversion from
the types of its parameters
to the type of its class.
Such a constructor is called a
\indexdefn{constructor!converting}%
\term{converting constructor}.
\enterexample

\indextext{Jessie}%
\begin{codeblock}
struct X {
    X(int);
    X(const char*, int =0);
    X(int, int);
};

void f(X arg) {
  X a = 1;          // \tcode{a = X(1)}
  X b = "Jessie";   // \tcode{b = X("Jessie",0)}
  a = 2;            // \tcode{a = X(2)}
  f(3);             // \tcode{f(X(3))}
  f({1, 2});        // \tcode{f(X(1,2))}
}
\end{codeblock}
\exitexample

\pnum
An explicit constructor constructs objects just like non-explicit
constructors, but does so only where the direct-initialization syntax~(\ref{dcl.init}) or where casts~(\ref{expr.static.cast}, \ref{expr.cast}) are explicitly
used.
A default constructor may be an explicit constructor; such a constructor
will be used to perform default-initialization
or value-initialization~(\ref{dcl.init}).
\enterexample

\begin{codeblock}
struct Z {
  explicit Z();
  explicit Z(int);
  explicit Z(int, int);
};

Z a;                            // OK: default-initialization performed
Z a1 = 1;                       // error: no implicit conversion
Z a3 = Z(1);                    // OK: direct initialization syntax used
Z a2(1);                        // OK: direct initialization syntax used
Z* p = new Z(1);                // OK: direct initialization syntax used
Z a4 = (Z)1;                    // OK: explicit cast used
Z a5 = static_cast<Z>(1);       // OK: explicit cast used
Z a6 = { 3, 4 };                // error: no implicit conversion
\end{codeblock}
\exitexample

\pnum
A
non-explicit
copy/move constructor~(\ref{class.copy}) is a converting constructor.
An implicitly-declared copy/move constructor is not an explicit constructor;
it may be called for implicit type conversions.

\rSec2[class.conv.fct]{Conversion functions}%
\indextext{function!conversion}%
\indextext{fundamental~type~conversion|see{conversion, user-defined}}%
\indextext{conversion!user-defined}%
\indextext{conversion operator|see{conversion, user defined}}

\pnum
A member function of a class \tcode{X} having no parameters with a name of the form

\begin{bnf}
\nontermdef{conversion-function-id}\br
    \terminal{operator} conversion-type-id
\end{bnf}

\begin{bnf}
\nontermdef{conversion-type-id}\br
    type-specifier-seq conversion-declarator\opt
\end{bnf}

\begin{bnf}
\nontermdef{conversion-declarator}\br
    ptr-operator conversion-declarator\opt
\end{bnf}

specifies a conversion from
\tcode{X}
to the type specified by the
\grammarterm{conversion-type-id}.
Such functions are called conversion functions.
No return type can be specified.
\indextext{conversion!type~of}%
If a conversion function is a member function, the type of the conversion function~(\ref{dcl.fct}) is
``function taking no parameter returning
\grammarterm{conversion-type-id}''.
A conversion function is never used to convert a (possibly cv-qualified) object
to the (possibly cv-qualified) same object type (or a reference to it),
to a (possibly cv-qualified) base class of that type (or a reference to it),
or to (possibly cv-qualified) void.\footnote{These conversions are considered
as standard conversions for the purposes of overload resolution~(\ref{over.best.ics}, \ref{over.ics.ref}) and therefore initialization~(\ref{dcl.init}) and explicit casts~(\ref{expr.static.cast}). A conversion to \tcode{void} does not invoke any conversion function~(\ref{expr.static.cast}).
Even though never directly called to perform a conversion,
such conversion functions can be declared and can potentially
be reached through a call to a virtual conversion function in a base class.}

\enterexample

\begin{codeblock}
struct X {
  operator int();
};

void f(X a) {
  int i = int(a);
  i = (int)a;
  i = a;
}
\end{codeblock}

In all three cases the value assigned will be converted by
\tcode{X::operator int()}.
\exitexample

\pnum
A conversion function may be explicit~(\ref{dcl.fct.spec}), in which case it is only considered as a user-defined conversion for direct-initialization~(\ref{dcl.init}). Otherwise, user-defined conversions are not restricted to use in assignments and initializations.
\enterexample

\begin{codeblock}
class Y { };
struct Z {
  explicit operator Y() const;
};

void h(Z z) {
  Y y1(z);          // OK: direct-initialization
  Y y2 = z;         // ill-formed: copy-initialization
  Y y3 = (Y)z;      // OK: cast notation
}

void g(X a, X b) {
  int i = (a) ? 1+a : 0;
  int j = (a&&b) ? a+b : i;
  if (a) {
  }
}
\end{codeblock}
\exitexample

\pnum
The
\grammarterm{conversion-type-id}
shall not represent a function type nor an array type.
The
\grammarterm{conversion-type-id}
in a
\grammarterm{conversion-function-id}
is the longest possible sequence of
\grammarterm{conversion-declarator}{s}.
\enternote
This prevents ambiguities between the declarator operator * and its expression
counterparts.
\enterexample

\begin{codeblock}
&ac.operator int*i; // syntax error:
                    // parsed as: \tcode{\&(ac.operator int *)i}
                    // not as: \tcode{\&(ac.operator int)*i}
\end{codeblock}

The \tcode{*} is the pointer declarator and not the multiplication operator.
\exitexample
\exitnote

\pnum
\indextext{conversion!inheritance~of user-defined}%
Conversion functions are inherited.

\pnum
\indextext{conversion!virtual user-defined}%
Conversion functions can be virtual.

\pnum
\indextext{conversion!static user-defined}%
Conversion functions cannot be declared
\tcode{static}.

\rSec1[class.dtor]{Destructors}%
\indextext{destructor}

\pnum
A declaration of a destructor uses a function declarator~(\ref{dcl.fct}) of the form

\begin{ncbnf}
ptr-declarator \terminal{(} parameter-declaration-clause \terminal{)} exception-specification\opt attribute-specifier-seq\opt
\end{ncbnf}

where the \grammarterm{ptr-declarator} consists solely of an
\grammarterm{id-expression}, an optional \grammarterm{attribute-specifier-seq},
and optional surrounding parentheses, and the \grammarterm{id-expression} has
one of the following forms:

\begin{itemize}
\item
in a \grammarterm{member-declaration} that belongs to the
\grammarterm{member-specification} of a class but is not a friend
declaration~(\ref{class.friend}), the \grammarterm{id-expression} is
\tcode{\~}\grammarterm{class-name} and the \grammarterm{class-name} is the
injected-class-name (Clause~\ref{class}) of the immediately-enclosing class;

\item
in a \grammarterm{member-declaration} that belongs to the
\grammarterm{member-specification} of a class template but is not a friend
declaration, the \grammarterm{id-expression} is
\tcode{\~}\grammarterm{class-name} and the \grammarterm{class-name} names the
current instantiation~(\ref{temp.dep.type}) of the immediately-enclosing class template; or

\item
in a declaration at namespace scope or in a friend declaration, the
\grammarterm{id-expression} is \grammarterm{nested-name-specifier}
\tcode{\~}\grammarterm{class-name} and the \grammarterm{class-name} names the
same class as the \grammarterm{nested-name-specifier}.
\end{itemize}

The \grammarterm{class-name} shall not be a \grammarterm{typedef-name}. A
destructor shall take no arguments (\ref{dcl.fct}). In a destructor
declaration, each \grammarterm{decl-specifier} of the optional
\grammarterm{decl-specifier-seq} shall be \tcode{friend}, \tcode{inline}, or
\tcode{virtual}.

\pnum
A destructor is used to destroy objects of its class type.
\indextext{restriction!destructor}%
The address of a destructor shall not be taken.
\indextext{\idxcode{const}!destructor~and}%
\indextext{\idxcode{volatile}!destructor~and}%
A destructor can be invoked for a
\tcode{const},
\tcode{volatile}
or
\tcode{const}
\tcode{volatile}
object.
\tcode{const}
and
\tcode{volatile}
semantics~(\ref{dcl.type.cv}) are not applied on an object under destruction.
They stop being in effect when the destructor for the
most derived object~(\ref{intro.object}) starts.

\pnum
A declaration of a destructor that does not have an \grammarterm{exception-specification}
is implicitly considered to have the same \grammarterm{exception-specification} as an
implicit declaration~(\ref{except.spec}).

\pnum
\indextext{generated~destructor|see{destructor, default}}%
\indextext{destructor!default}%
\indextext{destructor!non-trivial}%
If a class has no user-declared
destructor, a destructor is implicitly
declared as defaulted~(\ref{dcl.fct.def}).
An implicitly-declared destructor is an
\tcode{inline}
\tcode{public}
member of its class.

\pnum
A defaulted destructor for a class
  \tcode{X} is defined as deleted if:
\begin{itemize}
\item \tcode{X} is a union-like class that has a variant
  member with a non-trivial destructor,

\item any potentially constructed subobject has class type
  \tcode{M} (or array thereof) and
  \tcode{M} has a deleted destructor or a destructor
  that is inaccessible from the defaulted destructor,

\item or, for a virtual destructor, lookup of the non-array deallocation
  function results in an ambiguity or in a function that is deleted or
  inaccessible from the defaulted destructor.
\end{itemize}

A destructor is trivial if it is not user-provided and if:

\begin{itemize}
\item the destructor is not \tcode{virtual},

\item all of the direct base classes of its class have trivial destructors, and

\item for all of the non-static data members of its class that are of class
type (or array thereof), each such class has a trivial destructor.
\end{itemize}

Otherwise, the destructor is
\grammarterm{non-trivial}.

\pnum
\indextext{destructor!implicitly defined}%
A destructor
that is defaulted and not defined as deleted
is
\term{implicitly defined}
when it is odr-used~(\ref{basic.def.odr}) to destroy an object of its class type~(\ref{basic.stc})
or when it is explicitly defaulted after its first declaration.

\pnum
Before the
defaulted destructor for a class is implicitly defined, all the non-user-provided
destructors for its base classes and its non-static data members shall have been
implicitly defined.

\pnum
\indextext{order~of~execution!destructor}%
\indextext{order~of~execution!base~class destructor}%
\indextext{order~of~execution!member destructor}%
After executing the body of the destructor and destroying
any automatic objects allocated within the body, a
destructor for class
\tcode{X}
calls the destructors for
\tcode{X}'s
direct non-variant non-static data members, the destructors for
\tcode{X}'s
direct base classes and, if
\tcode{X}
is the type of the most derived class~(\ref{class.base.init}),
its destructor calls the destructors for
\tcode{X}'s
virtual base classes.
All destructors are called as if they were referenced with a qualified name,
that is, ignoring any possible virtual overriding destructors in more
derived classes.
Bases and members are destroyed in the reverse order of the completion of
their constructor (see~\ref{class.base.init}).
A
\tcode{return}
statement~(\ref{stmt.return}) in a destructor might not directly return to the
caller; before transferring control to the caller, the destructors for the
members and bases are called.
\indextext{order~of~execution!destructor~and array}%
Destructors for elements of an array are called in reverse order of their
construction (see~\ref{class.init}).

\pnum
\indextext{destructor!virtual}%
\indextext{destructor!pure virtual}%
A destructor can be declared
\tcode{virtual}~(\ref{class.virtual})
or pure
\tcode{virtual}~(\ref{class.abstract});
if any objects of that class or any derived class are created in the program,
the destructor shall be defined.
If a class has a base class with a virtual destructor, its  destructor
(whether user- or implicitly-declared) is virtual.

\pnum
\enternote
\indextext{member function!destructor~and}%
some language constructs have special semantics when used during destruction;
see~\ref{class.cdtor}.
\exitnote

\pnum
\indextext{destructor!implicit call}%
\indextext{destructor!program termination~and}%
A destructor is invoked implicitly

\begin{itemize}

\item for a constructed object with static storage duration~(\ref{basic.stc.static}) at program termination~(\ref{basic.start.term}),

\item for a constructed object with thread storage duration~(\ref{basic.stc.thread}) at thread exit,

\item for a constructed object with automatic storage duration~(\ref{basic.stc.auto}) when the block in which an object is created exits~(\ref{stmt.dcl}),

\item for a constructed temporary object when its lifetime ends~(\ref{class.temporary}).
\end{itemize}

\indextext{\idxcode{delete}!destructor~and}%
\indextext{destructor!explicit call}%
In each case, the context of the invocation is the context of the construction of
the object. A destructor is also invoked implicitly through use of a
\grammarterm{delete-expression}~(\ref{expr.delete}) for a constructed object allocated
by a \grammarterm{new-expression}~(\ref{expr.new}); the context of the invocation is the
\grammarterm{delete-expression}.
\enternote An array of class type contains several subobjects for each of which
the destructor is invoked. \exitnote
A destructor can also be invoked explicitly. A destructor is \term{potentially invoked}
if it is invoked or as specified in~\ref{expr.new} and~\ref{class.base.init}.
A program is ill-formed if a destructor that is potentially invoked is deleted
or not accessible from the context of the invocation.

\pnum
At the point of definition of a virtual destructor (including an implicit
definition~(\ref{class.copy})), the non-array deallocation function is looked up
in the scope of the destructor's class~(\ref{class.member.lookup}), and, if no
declaration is found, the function is looked up in the global scope.
If the result of this lookup is ambiguous or inaccessible, or if the lookup
selects a placement deallocation function or a function with a deleted definition~(\ref{dcl.fct.def}), the program is ill-formed.
\enternote
This assures that a deallocation function corresponding to the dynamic type of an
object is available for the
\grammarterm{delete-expression}~(\ref{class.free}).
\exitnote

\pnum
\indextext{destructor!explicit call}%
In an explicit destructor call, the destructor name appears as a
\tcode{\~{}}
followed by a
\grammarterm{type-name} or \grammarterm{decltype-specifier}
that denotes the destructor's class type.
The invocation of a destructor is subject to the usual rules for member
functions~(\ref{class.mfct});
that is, if the object is not of the destructor's class type and
not of a class derived from the destructor's class type (including when
the destructor is invoked via a null pointer value), the program has
undefined behavior.
\enternote invoking \tcode{delete} on a null pointer does not call the
destructor; see \ref{expr.delete}. \exitnote
\enterexample

\begin{codeblock}
struct B {
  virtual ~B() { }
};
struct D : B {
  ~D() { }
};

D D_object;
typedef B B_alias;
B* B_ptr = &D_object;

void f() {
  D_object.B::~B();             // calls \tcode{B}'s destructor
  B_ptr->~B();                  // calls \tcode{D}'s destructor
  B_ptr->~B_alias();            // calls \tcode{D}'s destructor
  B_ptr->B_alias::~B();         // calls \tcode{B}'s destructor
  B_ptr->B_alias::~B_alias();   // calls \tcode{B}'s destructor
}
\end{codeblock}
\exitexample
\enternote
An explicit destructor call must always be written using
a member access operator~(\ref{expr.ref}) or a qualified-id~(\ref{expr.prim});
in particular, the
\grammarterm{unary-expression}
\tcode{\~{}X()}
in a member function is not an explicit destructor call~(\ref{expr.unary.op}).
\exitnote

\pnum
\enternote
\indextext{object!destructor~and placement~of}%
explicit calls of destructors are rarely needed.
One use of such calls is for objects placed at specific
addresses using a
\grammarterm{new-expression}
with the placement option.
Such use of explicit placement and destruction of objects can be necessary
to cope with dedicated hardware resources and for writing memory management
facilities.
For example,
\indextext{example!explicit destructor~call}%

\begin{codeblock}
void* operator new(std::size_t, void* p) { return p; }
struct X {
  X(int);
  ~X();
};
void f(X* p);

void g() {                      // rare, specialized use:
  char* buf = new char[sizeof(X)];
  X* p = new(buf) X(222);       // use \tcode{buf[]} and initialize
  f(p);
  p->X::~X();                   // cleanup
}
\end{codeblock}
\exitnote

\pnum
Once a destructor is invoked for an object, the object no longer exists;
the behavior is undefined if the destructor is invoked
for an object whose lifetime has ended~(\ref{basic.life}).
\enterexample
if the destructor for an automatic object is explicitly invoked,
and the block is subsequently left in a manner that would ordinarily
invoke implicit destruction of the object, the behavior is undefined.
\exitexample

\pnum
\enternote
\indextext{fundamental~type!destructor~and}%
the notation for explicit call of a destructor can be used for any scalar type
name~(\ref{expr.pseudo}).
Allowing this makes it possible to write code without having to know if a
destructor exists for a given type.
For example,

\begin{codeblock}
typedef int I;
I* p;
p->I::~I();
\end{codeblock}
\exitnote

\rSec1[class.free]{Free store}%
\indextext{free store}%

\pnum
\indextext{\idxcode{new}!type~of}
Any allocation function for a class
\tcode{T}
is a static member (even if not explicitly declared
\tcode{static}).

\pnum
\enterexample

\begin{codeblock}
class Arena;
struct B {
  void* operator new(std::size_t, Arena*);
};
struct D1 : B {
};

Arena*  ap;
void foo(int i) {
  new (ap) D1;      // calls \tcode{B::operator new(std::size_t, Arena*)}
  new D1[i];        // calls \tcode{::operator new[](std::size_t)}
  new D1;           // ill-formed: \tcode{::operator new(std::size_t)} hidden
}
\end{codeblock}
\exitexample

\pnum
\indextext{\idxcode{delete}}%
When an object is deleted with a
\grammarterm{delete-expression}~(\ref{expr.delete}),
a \term{deallocation function}
\indextext{function!deallocation}%
(\tcode{operator delete()}
\indextext{\idxcode{operator delete}}%
for non-array objects or
\tcode{operator delete[]()}
\indextext{\idxcode{operator delete}}%
for arrays) is (implicitly) called to reclaim the storage occupied by
the object~(\ref{basic.stc.dynamic.deallocation}).

\pnum
Class-specific deallocation function lookup is a part of general deallocation
function lookup~(\ref{expr.delete}) and occurs as follows.
If the \grammarterm{delete-expression}
is used to deallocate a class object whose static type has a virtual
destructor, the deallocation function is the one selected at the point
of definition of the dynamic type's virtual
destructor~(\ref{class.dtor}).\footnote{A similar provision is not needed for
the array version of \tcode{operator} \tcode{delete} because~\ref{expr.delete}
requires that in this situation, the static type of the object to be deleted be
the same as its dynamic type.
}
Otherwise, if the
\grammarterm{delete-expression}
is used to deallocate an object of class
\tcode{T}
or array thereof, the static and dynamic types of the object shall be
identical and the deallocation function's name is looked up in the scope of
\tcode{T}.
If this lookup fails to find the name, general deallocation function
lookup~(\ref{expr.delete}) continues.
If the result of the lookup is ambiguous or inaccessible, or if the lookup
selects a placement deallocation function, the program is ill-formed.

\pnum
\indextext{\idxcode{delete}!type~of}%
Any deallocation function for a class
\tcode{X}
is a static member (even if not explicitly declared
\tcode{static}).
\indextext{example!\idxcode{delete}}%
\enterexample

\begin{codeblock}
class X {
  void operator delete(void*);
  void operator delete[](void*, std::size_t);
};

class Y {
  void operator delete(void*, std::size_t);
  void operator delete[](void*);
};
\end{codeblock}
\exitexample

\pnum
Since member allocation and deallocation functions are
\tcode{static}
they cannot be virtual.
\enternote
\indextext{example!destructor~and \tcode{delete}}%
\indextext{example!scope~of \tcode{delete}}%
however, when the
\grammarterm{cast-expression}
of a
\grammarterm{delete-expression}
refers to an object of class type,
because the deallocation function actually called is looked up in the scope of
the class that is the dynamic type of the object,
if the destructor is virtual, the effect is the same.
For example,

\begin{codeblock}
struct B {
  virtual ~B();
  void operator delete(void*, std::size_t);
};

struct D : B {
  void operator delete(void*);
};

void f() {
  B* bp = new D;
  delete bp;        //1: uses \tcode{D::operator delete(void*)}
}
\end{codeblock}

Here, storage for the non-array object of class
\tcode{D}
is deallocated by
\tcode{D::operator delete()},
due to the virtual destructor.
\exitnote
\enternote
Virtual destructors have no effect on the deallocation function actually
called when the
\grammarterm{cast-expression}
of a
\grammarterm{delete-expression}
refers to an array of objects of class type.
For example,

\begin{codeblock}
struct B {
  virtual ~B();
  void operator delete[](void*, std::size_t);
};

struct D : B {
  void operator delete[](void*, std::size_t);
};

void f(int i) {
  D* dp = new D[i];
  delete [] dp;     // uses \tcode{D::operator delete[](void*, std::size_t)}
  B* bp = new D[i];
  delete[] bp;      // undefined behavior
}
\end{codeblock}
\exitnote

\pnum
Access to the deallocation function is checked statically.
Hence, even though a different one might actually be executed,
the statically visible deallocation function is required to be accessible.
\enterexample
for the call on line //1 above,
if
\tcode{B::operator delete()}
had been
\tcode{private},
the delete expression would have been ill-formed.
\exitexample

\pnum
\enternote
If a deallocation function has no explicit \grammarterm{exception-specification}, it is
treated as if it were specified with \tcode{noexcept(true)}~(\ref{except.spec}).
\exitnote

\rSec1[class.init]{Initialization}%
\indextext{initialization!class~object|(}%
\indextext{initialization!default constructor and}%
\indextext{initialization!constructor~and}

\pnum
When no initializer is specified for an object of (possibly
cv-qualified) class type (or array thereof), or the initializer has
the form
\tcode{()},
the object is initialized as specified in~\ref{dcl.init}.

\pnum
An object of class type (or array thereof) can be explicitly initialized;
see~\ref{class.expl.init} and~\ref{class.base.init}.

\pnum
\indextext{order~of~execution!constructor~and array}%
When an array of class objects is initialized
(either explicitly or implicitly) and the elements are initialized by constructor,
the constructor shall be called for each element of the array,
following the subscript order; see~\ref{dcl.array}.
\enternote
Destructors for the array elements are called in reverse order of their
construction.
\exitnote

\rSec2[class.expl.init]{Explicit initialization}%
\indextext{initialization!explicit}%
\indextext{initialization!constructor~and}%

\pnum
An object of class type can be initialized with a parenthesized
\grammarterm{expression-list},
where the
\grammarterm{expression-list}
is construed as an argument list for a constructor
that is called to initialize the object.
Alternatively, a single
\grammarterm{assignment-expression}
can be specified as an
\grammarterm{initializer}
using the
\tcode{=}
form of initialization.
Either direct-initialization semantics or copy-initialization semantics apply;
see~\ref{dcl.init}.
\indextext{example!constructor~and initialization}%
\enterexample

\begin{codeblock}
struct complex {
  complex();
  complex(double);
  complex(double,double);
};

complex sqrt(complex,complex);

complex a(1);                   // initialize by a call of
                                // \tcode{complex(double)}
complex b = a;                  // initialize by a copy of \tcode{a}
complex c = complex(1,2);       // construct \tcode{complex(1,2)}
                                // using \tcode{complex(double,double)}
                                // copy/move it into \tcode{c}
complex d = sqrt(b,c);          // call \tcode{sqrt(complex,complex)}
                                // and copy/move the result into \tcode{d}
complex e;                      // initialize by a call of
                                // \tcode{complex()}
complex f = 3;                  // construct \tcode{complex(3)} using
                                // \tcode{complex(double)}
                                // copy/move it into \tcode{f}
complex g = { 1, 2 };           // initialize by a call of
                                // \tcode{complex(double, double)}
\end{codeblock}
\exitexample
\enternote
\indextext{initialization!overloaded assignment~and}%
overloading of the assignment operator~(\ref{over.ass})
has no effect on initialization.
\exitnote

\pnum
\indextext{initialization!array~of class~objects}%
\indextext{constructor!array~of class~objects~and}%
An object of class type can also be initialized by a
\grammarterm{braced-init-list}. List-initialization semantics apply;
see~\ref{dcl.init} and~\ref{dcl.init.list}. \enterexample

\begin{codeblock}
complex v[6] = { 1, complex(1,2), complex(), 2 };
\end{codeblock}

Here,
\tcode{complex::complex(double)}
is called for the initialization of
\tcode{v[0]}
and
\tcode{v[3]},
\tcode{complex::complex(\brk{}double, double)}
is called for the initialization of
\tcode{v[1]},
\tcode{complex::complex()}
is called for the initialization
\tcode{v[2]},
\tcode{v[4]},
and
\tcode{v[5]}.
For another example,

\begin{codeblock}
struct X {
  int i;
  float f;
  complex c;
} x = { 99, 88.8, 77.7 };
\end{codeblock}

Here,
\tcode{x.i}
is initialized with 99,
\tcode{x.f}
is initialized with 88.8, and
\tcode{complex::complex(double)}
is called for the initialization of
\tcode{x.c}.
\exitexample
\enternote
Braces can be elided in the
\grammarterm{initializer-list}
for any aggregate, even if the aggregate has members of a class type with
user-defined type conversions; see~\ref{dcl.init.aggr}.
\exitnote

\pnum
\enternote
If
\tcode{T}
is a class type with no default constructor,
any declaration of an object of type
\tcode{T}
(or array thereof) is ill-formed if no
\grammarterm{initializer}
is explicitly specified (see~\ref{class.init} and~\ref{dcl.init}).
\exitnote

\pnum
\enternote
\indextext{order~of~execution!constructor~and \tcode{static}~objects}%
the order in which objects with static or thread storage duration
are initialized is described in~\ref{basic.start.init} and~\ref{stmt.dcl}.
\exitnote

\rSec2[class.base.init]{Initializing bases and members}%
\indextext{initialization!base~class}%
\indextext{initialization!member}

\pnum
In the definition of a constructor for a class,
initializers for direct and virtual base subobjects and
non-static data members can be specified by a
\grammarterm{ctor-initializer},
which has the form

\begin{bnf}
\nontermdef{ctor-initializer}\br
    \terminal{:} mem-initializer-list
\end{bnf}

\begin{bnf}
\nontermdef{mem-initializer-list}\br
    mem-initializer \terminal{...}\opt\br
    mem-initializer \terminal{...}\opt \terminal{,} mem-initializer-list
\end{bnf}

\begin{bnf}
\nontermdef{mem-initializer}\br
    mem-initializer-id \terminal{(} expression-list\opt \terminal{)}\br
    mem-initializer-id braced-init-list
\end{bnf}

\begin{bnf}
\nontermdef{mem-initializer-id}\br
    class-or-decltype\br
    identifier
\end{bnf}

\pnum
In a \grammarterm{mem-initializer-id} an initial unqualified
\grammarterm{identifier} is looked up in the scope of the constructor's class
and, if not found in that scope, it is looked up in the scope containing the
constructor's definition.
\enternote
If the constructor's class contains a member with the same name as a direct
or virtual base class of the class, a
\grammarterm{mem-initializer-id}
naming the member or base class and composed of a single identifier
refers to the class member.
A
\grammarterm{mem-initializer-id}
for the hidden base class may be specified using a qualified name.
\exitnote
Unless the
\grammarterm{mem-initializer-id}
names the constructor's class,
a non-static data member of the constructor's class, or
a direct or virtual base of that class,
the
\grammarterm{mem-initializer}
is ill-formed.

\pnum
A
\grammarterm{mem-initializer-list}
can initialize a base class using any \grammarterm{class-or-decltype} that denotes that base class type.
\enterexample

\begin{codeblock}
struct A { A(); };
  typedef A global_A;
  struct B { };
  struct C: public A, public B { C(); };
  C::C(): global_A() { }        // mem-initializer for base \tcode{A}
\end{codeblock}
\exitexample

\pnum
If a
\grammarterm{mem-initializer-id}
is ambiguous because it designates both a direct non-virtual base class and
an inherited virtual base class, the
\grammarterm{mem-initializer}
is ill-formed.
\enterexample

\begin{codeblock}
struct A { A(); };
struct B: public virtual A { };
struct C: public A, public B { C(); };
C::C(): A() { }                 // ill-formed: which \tcode{A}?
\end{codeblock}
\exitexample

\pnum
A
\grammarterm{ctor-initializer}
may initialize a variant member of the
constructor's class.
If a
\grammarterm{ctor-initializer}
specifies more than one
\grammarterm{mem-initializer}
for the same member or for the same base class,
the
\grammarterm{ctor-initializer}
is ill-formed.

\pnum
A \grammarterm{mem-initializer-list} can delegate to another
constructor of the constructor's class using any
\grammarterm{class-or-decltype} that denotes the constructor's class itself. If a
\nonterminal{mem-initializer-id} designates the constructor's class,
it shall be the only \nonterminal{mem-initializer}; the constructor
is a \term{delegating constructor}, and the constructor selected by the
\nonterminal{mem-initializer} is the \term{target constructor}.
The \term{principal constructor} is the first constructor invoked in
the construction of an object (that is, not a target constructor for that
object's construction). The target constructor is selected by overload resolution.
Once the target constructor returns, the body of the delegating constructor
is executed. If a constructor delegates to itself directly or indirectly,
the program is ill-formed; no diagnostic is required. \enterexample

\begin{codeblock}
struct C {
  C( int ) { }                  // \#1: non-delegating constructor
  C(): C(42) { }                // \#2: delegates to \#1
  C( char c ) : C(42.0) { }     // \#3: ill-formed due to recursion with \#4
  C( double d ) : C('a') { }    // \#4: ill-formed due to recursion with \#3
};
\end{codeblock}
\exitexample

\pnum
\indextext{initialization!base~class}%
\indextext{initialization!member~object}%
The
\grammarterm{expression-list}
or \grammarterm{braced-init-list}
in a
\grammarterm{mem-initializer}
is used to initialize the
designated subobject (or, in the case of a delegating constructor, the complete class object)
according to the initialization rules of~\ref{dcl.init} for direct-initialization.

\enterexample

\begin{codeblock}
struct B1 { B1(int); /* ... */ };
struct B2 { B2(int); /* ... */ };
struct D : B1, B2 {
  D(int);
  B1 b;
  const int c;
};

D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4)
  { /* ... */ }
D d(10);
\end{codeblock}
\exitexample
The initialization
performed by each \grammarterm{mem-initializer}
constitutes a full-expression.
Any expression in
a
\grammarterm{mem-initializer}
is evaluated as part of the full-expression that performs the initialization.
A \grammarterm{mem-initializer} where the \grammarterm{mem-initializer-id} denotes
a virtual base class is ignored during execution of a constructor of any class that is
not the most derived class.

\pnum
In a non-delegating constructor, if
a given potentially constructed subobject is not designated by a
\grammarterm{mem-initializer-id}
(including the case where there is no
\grammarterm{mem-initializer-list}
because the constructor has no
\grammarterm{ctor-initializer}),
then

\begin{itemize}
\item if the entity is a non-static data member that has a \grammarterm{brace-or-equal-initializer} and either

\begin{itemize}
\item the constructor's class is a union~(\ref{class.union}), and no other variant
member of that union is designated by a \grammarterm{mem-initializer-id} or

\item the constructor's class is not a union, and, if the entity is a member of an
anonymous union, no other member of that union is designated by a
\grammarterm{mem-initializer-id},
\end{itemize}

the entity is initialized as specified in~\ref{dcl.init};

\item otherwise, if the entity is an anonymous union or a variant member~(\ref{class.union}), no initialization is performed;

\item otherwise, the entity is default-initialized~(\ref{dcl.init}).
\end{itemize}

\enternote An abstract class~(\ref{class.abstract}) is never a most derived
class, thus its constructors never initialize virtual base classes, therefore the
corresponding \grammarterm{mem-initializer}{s} may be omitted. \exitnote
An attempt to initialize more than one non-static data member of a union renders the
program ill-formed.
\indextext{initialization!const member}%
\indextext{initialization!reference member}%
\enternote
After the call to a constructor for class
\tcode{X}
for an object with automatic or dynamic storage duration
has completed, if
the constructor was not invoked as part of value-initialization and
a member of
\tcode{X}
is neither initialized nor
given a value
during execution of the \grammarterm{compound-statement} of the body of the constructor,
the member has an indeterminate value.
\exitnote
\enterexample
\begin{codeblock}
struct A {
  A();
};

struct B {
  B(int);
};

struct C {
  C() { }               // initializes members as follows:
  A a;                    // OK: calls \tcode{A::A()}
  const B b;              // error: \tcode{B} has no default constructor
  int i;                  // OK: \tcode{i} has indeterminate value
  int j = 5;              // OK: \tcode{j} has the value \tcode{5}
};
\end{codeblock}
\exitexample

\pnum
If a given non-static data member has both a \grammarterm{brace-or-equal-initializer}
and a \grammarterm{mem-initializer}, the initialization specified by the
\grammarterm{mem-initializer} is performed, and the non-static data member's 
\grammarterm{brace-or-equal-initializer} is ignored.
\enterexample Given
\begin{codeblock}
struct A {
  int i = /* some integer expression with side effects */ ;
  A(int arg) : i(arg) { }
  // ...
};
\end{codeblock}

the \tcode{A(int)} constructor will simply initialize \tcode{i} to the value of
\tcode{arg}, and the
\indextext{side effects}%
side effects in \tcode{i}'s \grammarterm{brace-or-equal-initializer}
will not take place.
\exitexample

\pnum
In a non-delegating constructor, the destructor for each potentially constructed
subobject of class type is potentially invoked~(\ref{class.dtor}).
\enternote This provision ensures that destructors can be called for fully-constructed
sub-objects in case an exception is thrown~(\ref{except.ctor}). \exitnote

\pnum
In a non-delegating constructor, initialization
proceeds in the following order:

\begin{itemize}
\item
\indextext{initialization!order~of virtual~base~class}%
First, and only for the constructor of the most derived class~(\ref{intro.object}),
virtual base classes are initialized in the order they appear on a
depth-first left-to-right traversal of the directed acyclic graph of
base classes,
where ``left-to-right'' is the order of appearance of the base classes
in the derived class
\grammarterm{base-specifier-list}.
\item
\indextext{initialization!order~of base~class}%
Then, direct base classes are initialized in declaration order
as they appear in the
\grammarterm{base-specifier-list}
(regardless of the order of the
\grammarterm{mem-initializers}).
\item
\indextext{initialization!order~of member}%
Then, non-static data members are initialized in the order
they were declared in the class definition
(again regardless of the order of the
\grammarterm{mem-initializers}).
\item
Finally, the \grammarterm{compound-statement} of the constructor
body  is executed.
\end{itemize}

\enternote
The declaration order is mandated to ensure that base and member
subobjects are destroyed in the reverse order of initialization.
\exitnote

\pnum
\enterexample
\begin{codeblock}
struct V {
  V();
  V(int);
};

struct A : virtual V {
  A();
  A(int);
};

struct B : virtual V {
  B();
  B(int);
};

struct C : A, B, virtual V {
  C();
  C(int);
};

A::A(int i) : V(i) { /* ... */ }
B::B(int i) { /* ... */ }
C::C(int i) { /* ... */ }

V v(1);             // use \tcode{V(int)}
A a(2);             // use \tcode{V(int)}
B b(3);             // use \tcode{V()}
C c(4);             // use \tcode{V()}
\end{codeblock}
\exitexample

\pnum
\indextext{initializer!scope~of member}%
Names in the
\grammarterm{expression-list}
or \grammarterm{braced-init-list}
of a
\grammarterm{mem-initializer}
are evaluated in the scope of the constructor for which the
\grammarterm{mem-initializer}
is specified.
\enterexample

\begin{codeblock}
class X {
  int a;
  int b;
  int i;
  int j;
public:
  const int& r;
  X(int i): r(a), b(i), i(i), j(this->i) { }
};
\end{codeblock}

initializes
\tcode{X::r}
to refer to
\tcode{X::a},
initializes
\tcode{X::b}
with the value of the constructor parameter
\tcode{i},
initializes
\tcode{X::i}
with the value of the constructor parameter
\tcode{i},
and initializes
\tcode{X::j}
with the value of
\tcode{X::i};
this takes place each time an object of class
\tcode{X}
is created.
\exitexample
\enternote
Because the
\grammarterm{mem-initializer}
are evaluated in the scope of the constructor, the
\tcode{this}
pointer can be used in the
\grammarterm{expression-list}
of a
\grammarterm{mem-initializer}
to refer to the object being initialized.
\exitnote

\pnum
\indextext{initialization!member function call during}%
Member functions (including virtual member functions, \ref{class.virtual}) can be
called for an object under construction.
Similarly, an object under construction can be the operand of the
\tcode{typeid}
operator~(\ref{expr.typeid}) or of a
\tcode{dynamic_cast}~(\ref{expr.dynamic.cast}).
However, if these operations are performed in a
\grammarterm{ctor-initializer}
(or in a function called directly or indirectly from a
\grammarterm{ctor-initializer})
before all the
\grammarterm{mem-initializer}{s}
for base classes have completed, the result of the operation is undefined.
\enterexample

\begin{codeblock}
class A {
public:
  A(int);
};

class B : public A {
  int j;
public:
  int f();
  B() : A(f()),     // undefined: calls member function
                    // but base \tcode{A} not yet initialized
  j(f()) { }        // well-defined: bases are all initialized
};

class C {
public:
  C(int);
};

class D : public B, C {
  int i;
public:
  D() : C(f()),     // undefined: calls member function
                    // but base \tcode{C} not yet initialized
  i(f()) { }        // well-defined: bases are all initialized
};
\end{codeblock}
\exitexample

\pnum
\enternote
\ref{class.cdtor} describes the result of virtual function calls,
\tcode{typeid}
and
\tcode{dynamic_cast}s
during construction for the well-defined cases;
that is, describes the
\term{polymorphic behavior}
of an object under construction.
\exitnote

\pnum
\indextext{initializer!pack expansion}%
A \grammarterm{mem-initializer} followed by an ellipsis is
a pack expansion~(\ref{temp.variadic}) that initializes the base
classes specified by a pack expansion in the \grammarterm{base-specifier-list}
for the class. \enterexample

\begin{codeblock}
template<class... Mixins>
class X : public Mixins... {
public:
  X(const Mixins&... mixins) : Mixins(mixins)... { }
};
\end{codeblock}

\exitexample%
\indextext{initialization!class~object|)}

\rSec1[class.cdtor]{Construction and destruction}%
\indextext{construction|(}%
\indextext{destruction|(}

\pnum
\indextext{construction!member access}%
\indextext{destruction!member access}%
For an object with a non-trivial constructor, referring to any non-static member
or base class of the object before the constructor begins execution results in
undefined behavior. For an object with a non-trivial destructor, referring to
any non-static member or base class of the object after the destructor finishes
execution results in undefined behavior.
\enterexample

\begin{codeblock}
struct X { int i; };
struct Y : X { Y(); };                  // non-trivial
struct A { int a; };
struct B : public A { int j; Y y; };    // non-trivial

extern B bobj;
B* pb = &bobj;                          // OK
int* p1 = &bobj.a;                      // undefined, refers to base class member
int* p2 = &bobj.y.i;                    // undefined, refers to member's member

A* pa = &bobj;                          // undefined, upcast to a base class type
B bobj;                                 // definition of \tcode{bobj}

extern X xobj;
int* p3 = &xobj.i;                      //OK, \tcode{X} is a trivial class
X xobj;
\end{codeblock}

\pnum
For another example,

\begin{codeblock}
struct W { int j; };
struct X : public virtual W { };
struct Y {
  int* p;
  X x;
  Y() : p(&x.j) {   // undefined, \tcode{x} is not yet constructed
    }
};
\end{codeblock}
\exitexample

\pnum
\indextext{construction!pointer to member or base}%
\indextext{destruction!pointer to member or base}%
To explicitly or implicitly convert a pointer (a glvalue) referring to
an object of class
\tcode{X}
to a pointer (reference) to a direct or indirect base class
\tcode{B}
of
\tcode{X},
the construction of
\tcode{X}
and the construction of all of its direct or indirect bases that directly or
indirectly derive from
\tcode{B}
shall have started and the destruction of these classes shall not have
completed, otherwise the conversion results in undefined behavior.
To form a pointer to (or access the value of) a direct non-static member of
an object
\tcode{obj},
the construction of
\tcode{obj}
shall have started and its destruction shall not have completed,
otherwise the computation of the pointer value (or accessing the member
value) results in undefined behavior.
\enterexample

\begin{codeblock}
struct A { };
struct B : virtual A { };
struct C : B { };
struct D : virtual A { D(A*); };
struct X { X(A*); };

struct E : C, D, X {
  E() : D(this),    // undefined: upcast from \tcode{E*} to \tcode{A*}
                    // might use path \tcode{E*} $\rightarrow$ \tcode{D*} $\rightarrow$ \tcode{A*}
                    // but \tcode{D} is not constructed
                    // \tcode{D((C*)this)}, // defined:
                    // \tcode{E*} $\rightarrow$ \tcode{C*} defined because \tcode{E()} has started
                    // and \tcode{C*} $\rightarrow$ \tcode{A*} defined because
                    // \tcode{C} fully constructed
  X(this) {         // defined: upon construction of \tcode{X},
                    // \tcode{C/B/D/A} sublattice is fully constructed
  }
};
\end{codeblock}
\exitexample

\pnum
\indextext{virtual~function~call!constructor~and}%
\indextext{virtual~function~call!destructor~and}%
\indextext{construction!virtual function call}%
\indextext{destruction!virtual function call}%
Member functions, including virtual functions~(\ref{class.virtual}), can be called
during construction or destruction~(\ref{class.base.init}).
When a virtual function is called directly or indirectly from a constructor
or from a destructor,
including during the construction or destruction of the class's non-static data
members,
and the object to which the call applies is the object (call it \tcode{x}) under construction or
destruction,
the function called is the
final overrider in the constructor's or destructor's class and not one
overriding it in a more-derived class.
If the virtual function call uses an explicit class member access~(\ref{expr.ref})
and the object expression refers to
the complete object of \tcode{x} or one of that object's base class subobjects
but not \tcode{x} or one of its base class subobjects, the behavior
is undefined.
\enterexample

\begin{codeblock}
struct V {
  virtual void f();
  virtual void g();
};

struct A : virtual V {
  virtual void f();
};

struct B : virtual V {
  virtual void g();
  B(V*, A*);
};

struct D : A, B {
  virtual void f();
  virtual void g();
  D() : B((A*)this, this) { }
};

B::B(V* v, A* a) {
  f();              // calls \tcode{V::f}, not \tcode{A::f}
  g();              // calls \tcode{B::g}, not \tcode{D::g}
  v->g();           // \tcode{v} is base of \tcode{B}, the call is well-defined, calls \tcode{B::g}
  a->f();           // undefined behavior, \tcode{a}'s type not a base of \tcode{B}
}
\end{codeblock}
\exitexample

\pnum
\indextext{construction!\idxcode{typeid} operator}%
\indextext{destruction!\idxcode{typeid} operator}%
\indextext{\idxcode{typeid}!construction and}%
\indextext{\idxcode{typeid}!destruction and}%
The
\tcode{typeid}
operator~(\ref{expr.typeid}) can be used during construction or destruction~(\ref{class.base.init}).
When
\tcode{typeid}
is used in a constructor (including the
\grammarterm{mem-initializer} or \grammarterm{brace-or-equal-initializer}
for a non-static data member)
or in a destructor, or used in a function called (directly or indirectly) from
a constructor or destructor, if the operand of
\tcode{typeid}
refers to the object under construction or destruction,
\tcode{typeid}
yields the
\tcode{std::type_info}
object representing the constructor or destructor's class.
If the operand of
\tcode{typeid}
refers to the object under construction or destruction and the static type of
the operand is neither the constructor or destructor's class nor one of its
bases, the result of
\tcode{typeid}
is undefined.

\pnum
\indextext{construction!dynamic cast and}%
\indextext{destruction!dynamic cast and}%
\indextext{cast!dynamic!construction and}%
\indextext{cast!dynamic!destruction and}%
\tcode{dynamic_cast}s~(\ref{expr.dynamic.cast}) can be used during construction
or destruction~(\ref{class.base.init}). When a
\tcode{dynamic_cast}
is used in a constructor (including the
\grammarterm{mem-initializer} or \grammarterm{brace-or-equal-initializer}
for a non-static data member)
or in a destructor, or used in a function called (directly or indirectly) from
a constructor or destructor, if the operand of the
\tcode{dynamic_cast}
refers to the object under construction or destruction, this object is
considered to be a most derived object that has the type of the constructor or
destructor's class.
If the operand of the
\tcode{dynamic_cast}
refers to the object under construction or destruction and the static type of
the operand is not a pointer to or object of the constructor or destructor's
own class or one of its bases, the
\tcode{dynamic_cast}
results in undefined behavior.

\enterexample

\begin{codeblock}
struct V {
  virtual void f();
};

struct A : virtual V { };

struct B : virtual V {
  B(V*, A*);
};

struct D : A, B {
  D() : B((A*)this, this) { }
};

B::B(V* v, A* a) {
  typeid(*this);                // \tcode{type_info} for \tcode{B}
  typeid(*v);                   // well-defined: \tcode{*v} has type \tcode{V}, a base of \tcode{B}
                                // yields \tcode{type_info} for \tcode{B}
  typeid(*a);                   // undefined behavior: type \tcode{A} not a base of \tcode{B}
  dynamic_cast<B*>(v);          // well-defined: \tcode{v} of type \tcode{V*}, \tcode{V} base of \tcode{B}
                                // results in \tcode{B*}
  dynamic_cast<B*>(a);          // undefined behavior,
                                // \tcode{a} has type \tcode{A*}, \tcode{A} not a base of \tcode{B}
}
\end{codeblock}
\exitexample%
\indextext{destruction|)}%
\indextext{construction|)}

\rSec1[class.copy]{Copying and moving class objects}%
\indextext{copy!class~object|see{constructor, copy; assignment, copy}}%
\indextext{move!class~object|see{constructor, move; assignment, move}}%
\indextext{constructor!copy}%
\indextext{constructor!move}%
\indextext{operator!copy assignment|see{assignment, copy}}%
\indextext{operator!move assignment|see{assignment, move}}

\pnum
A class object can be copied or moved in two ways:
by initialization~(\ref{class.ctor}, \ref{dcl.init}), including for function argument passing~(\ref{expr.call}) and for function value return~(\ref{stmt.return});
and by assignment~(\ref{expr.ass}).
Conceptually, these two operations are implemented by a
copy/move constructor~(\ref{class.ctor})
and copy/move assignment operator~(\ref{over.ass}).

\pnum
\indextext{constructor!copy|(}%
\indextext{constructor!move|(}%
A non-template constructor for class
\tcode{X}
is
a
copy
constructor if its first parameter is of type
\tcode{X\&},
\tcode{const X\&},
\tcode{volatile X\&}
or
\tcode{const volatile X\&},
and either there are no other parameters
or else all other parameters have default arguments~(\ref{dcl.fct.default}).
\enterexample
\tcode{X::X(const X\&)}
and
\tcode{X::X(X\&,int=1)}
are copy constructors.

\begin{codeblock}
struct X {
  X(int);
  X(const X&, int = 1);
};
X a(1);             // calls \tcode{X(int);}
X b(a, 0);          // calls \tcode{X(const X\&, int);}
X c = b;            // calls \tcode{X(const X\&, int);}
\end{codeblock}
\exitexample

\pnum
A non-template constructor for class \tcode{X} is a move constructor if its
first parameter is of type \tcode{X\&\&}, \tcode{const X\&\&},
\tcode{volatile X\&\&}, or \tcode{const volatile X\&\&}, and either there are
no other parameters or else all other parameters have default 
arguments~(\ref{dcl.fct.default}).
\enterexample \tcode{Y::Y(Y\&\&)} is a move constructor.
\begin{codeblock}
struct Y {
  Y(const Y&);
  Y(Y&&);
};
extern Y f(int);
Y d(f(1));          // calls \tcode{Y(Y\&\&)}
Y e = d;            // calls \tcode{Y(const Y\&)}
\end{codeblock}
\exitexample

\pnum
\enternote
All forms of copy/move constructor may be declared for a class.
\enterexample

\begin{codeblock}
struct X {
  X(const X&);
  X(X&);            // OK
  X(X&&);
  X(const X&&);     // OK, but possibly not sensible
};
\end{codeblock}
\exitexample
\exitnote

\pnum
\enternote
If a class
\tcode{X}
only has a copy constructor with a parameter of type
\tcode{X\&},
an initializer of type
\tcode{const}
\tcode{X}
or
\tcode{volatile}
\tcode{X}
cannot initialize an object of type
(possibly
cv-qualified)
\tcode{X}.
\enterexample

\begin{codeblock}
struct X {
  X();              // default constructor
  X(X&);            // copy constructor with a nonconst parameter
};
const X cx;
X x = cx;           // error: \tcode{X::X(X\&)} cannot copy \tcode{cx} into \tcode{x}
\end{codeblock}
\exitexample
\exitnote

\pnum
A declaration of a constructor for a class
\tcode{X}
is ill-formed if its first parameter is of type (optionally cv-qualified)
\tcode{X}
and either there are no other parameters or else all other parameters have
default arguments.
A member function template is never instantiated to
produce such a constructor signature.
\enterexample
\begin{codeblock}
struct S {
  template<typename T> S(T);
  S();
};

S g;

void h() {
  S a(g);           // does not instantiate the member template to produce \tcode{S::S<S>(S)};
                    // uses the implicitly declared copy constructor
}
\end{codeblock}
\exitexample

\pnum
\indextext{constructor!copy!implicitly declared}%
If the class definition does not explicitly declare a copy constructor,
one is declared \term{implicitly}. If the class definition declares a move
constructor or move assignment operator, the implicitly declared copy
constructor is defined as deleted; otherwise, it is defined as
defaulted~(\ref{dcl.fct.def}).
The latter case is deprecated if the class has a user-declared copy assignment
operator or a user-declared destructor.

\pnum
The implicitly-declared copy constructor for a class
\tcode{X}
will have the form

\begin{codeblock}
X::X(const X&)
\end{codeblock}

if each potentially constructed subobject of a class type
\tcode{M}
(or array thereof)
has a copy constructor whose first parameter is of type
\tcode{const}
\tcode{M\&}
or
\tcode{const}
\tcode{volatile}
\tcode{M\&}.\footnote{This implies that the reference parameter of the
implicitly-declared copy constructor
cannot bind to a
\tcode{volatile}
lvalue; see~\ref{diff.special}.}
Otherwise, the implicitly-declared copy constructor will have the form

\begin{codeblock}
X::X(X&)
\end{codeblock}

\pnum
\indextext{constructor!move!implicitly declared}%
If the definition of a class \tcode{X} does not explicitly declare
a move constructor, one will be
implicitly declared as defaulted if and only if

\begin{itemize}
\item
\tcode{X} does not have a user-declared copy constructor,

\item
\tcode{X} does not have a user-declared copy assignment operator,

\item
\tcode{X} does not have a user-declared move assignment operator, and

\item
\tcode{X} does not have a user-declared destructor.
\end{itemize}

\enternote When the move constructor is not implicitly declared or explicitly supplied,
expressions that otherwise would have invoked the move constructor may instead invoke
a copy constructor. \exitnote

\pnum
The implicitly-declared move constructor for class \tcode{X} will have the form
\begin{codeblock}
X::X(X&&)
\end{codeblock}

\pnum
An implicitly-declared copy/move constructor is an
\tcode{inline}
\tcode{public}
member of its class.
A defaulted copy/\brk{}move constructor for a class
  \tcode{X} is defined as deleted~(\ref{dcl.fct.def.delete}) if \tcode{X} has:
\begin{itemize}
\item a variant member with a non-trivial corresponding constructor and
  \tcode{X} is a union-like class,

\item a potentially constructed subobject type
  \tcode{M} (or array thereof) that cannot be copied/moved because
  overload resolution (\ref{over.match}), as applied to
  \tcode{M}'s 
  corresponding constructor, results in an ambiguity or 
  a function that is deleted or inaccessible from the
  defaulted constructor,

\item any potentially constructed subobject of a type
  with a destructor that is deleted or inaccessible from the defaulted
  constructor, or,

\item for the copy constructor, a non-static data member of rvalue reference type.
\end{itemize}

A defaulted move constructor that is defined as deleted is ignored by overload
resolution~(\ref{over.match}, \ref{over.over}).
\enternote
A deleted move constructor would otherwise interfere with initialization from
an rvalue which can use the copy constructor instead.
\exitnote

\pnum
\indextext{constructor!copy!trivial}%
\indextext{constructor!move!trivial}%
A copy/move constructor for class
\tcode{X}
is
trivial
if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list
of an implicit declaration, and if

\begin{itemize}
\item
class
\tcode{X}
has no virtual functions~(\ref{class.virtual})
and no virtual base classes~(\ref{class.mi}), and

\item class \tcode{X} has no non-static data members of volatile-qualified type, and

\item
the constructor selected to copy/move each direct base class subobject is trivial, and

\item
for each non-static data member of
\tcode{X}
that is of class type (or array thereof),
the constructor selected to copy/move that member is trivial;
\end{itemize}

otherwise the copy/move constructor is
\grammarterm{non-trivial}.

\pnum
\indextext{constructor!copy!implicitly defined}%
\indextext{constructor!move!implicitly defined}%
A copy/move constructor
that is defaulted and not defined as deleted
is
\term{implicitly defined}
if it is odr-used~(\ref{basic.def.odr})
or when it is explicitly defaulted after its first declaration.
\enternote
The copy/move constructor is implicitly defined even if the implementation elided
its odr-use (\ref{basic.def.odr}, \ref{class.temporary}).
\exitnote
If the implicitly-defined constructor would satisfy the requirements of a
\tcode{constexpr} constructor~(\ref{dcl.constexpr}), the implicitly-defined
constructor is \tcode{constexpr}.

\pnum
Before the defaulted copy/move constructor for a class is
implicitly defined,
all non-user-provided copy/move constructors for its
potentially constructed subobjects
shall have been implicitly defined.
\enternote
An implicitly-declared copy/move constructor has an
\grammarterm{exception-specification}~(\ref{except.spec}).
\exitnote

\pnum
The implicitly-defined copy/move constructor for a non-union class
\tcode{X}
performs a memberwise copy/move of its bases and members.
\enternote \grammarterm{brace-or-equal-initializer}{s} of non-static data members are ignored. See also the example in~\ref{class.base.init}. \exitnote
The order of initialization is the same as the order of initialization of bases
and members in a user-defined constructor (see~\ref{class.base.init}).
Let \tcode{x} be either the parameter of the constructor or, for the move constructor, an
xvalue referring to the parameter.
Each base or non-static data member
is copied/moved in the manner appropriate to its type:

\begin{itemize}
\item
if the member is an array, each element is
direct-initialized with the corresponding subobject of \tcode{x};

\item
if a member \tcode{m} has rvalue reference type \tcode{T\&\&}, it is direct-initialized with
\tcode{static_cast<T\&\&>(x.m)};

\item
otherwise, the base or member is direct-initialized with the corresponding base or member of \tcode{x}.
\end{itemize}

\indextext{initialization!virtual~base~class}%
Virtual base class subobjects shall be initialized only once by
the implicitly-defined copy/move constructor (see~\ref{class.base.init}).

\pnum
The implicitly-defined copy/move constructor for a union
\tcode{X} copies the object representation~(\ref{basic.types}) of \tcode{X}.%
\indextext{constructor!move|)}%
\indextext{constructor!copy|)}

\pnum
\indextext{assignment operator!copy|(}%
\indextext{assignment operator!move|(}%
A user-declared \term{copy} assignment operator \tcode{X::operator=} is a
non-static non-template member function of class \tcode{X} with exactly one
parameter of type \tcode{X}, \tcode{X\&}, \tcode{const} \tcode{X\&},
\tcode{volatile} \tcode{X\&} or \tcode{const} \tcode{volatile}
\tcode{X\&}.\footnote{Because a template assignment operator or an assignment
operator taking an rvalue reference parameter is never a copy assignment
operator, the presence of such an assignment operator does not suppress the
implicit declaration of a copy assignment operator. Such assignment operators
participate in overload resolution with other assignment operators, including
copy assignment operators, and, if selected, will be used to assign an object.}
\enternote
An overloaded assignment operator must be declared to have only one parameter;
see~\ref{over.ass}.
\exitnote
\enternote
More than one form of copy assignment operator may be declared for a class.
\exitnote
\enternote
If a class
\tcode{X}
only has a copy assignment operator with a parameter of type
\tcode{X\&},
an expression of type const
\tcode{X}
cannot be assigned to an object of type
\tcode{X}.
\enterexample

\begin{codeblock}
struct X {
  X();
  X& operator=(X&);
};
const X cx;
X x;
void f() {
  x = cx;           // error: \tcode{X::operator=(X\&)} cannot assign \tcode{cx} into \tcode{x}
}
\end{codeblock}
\exitexample
\exitnote

\pnum
\indextext{assignment operator!copy!implicitly declared}%
If the class definition does not explicitly declare a copy assignment operator,
one is declared \term{implicitly}. If the class definition declares a move
constructor or move assignment operator, the implicitly declared copy
assignment operator is defined as deleted; otherwise, it is defined as
defaulted~(\ref{dcl.fct.def}).
The latter case is deprecated if the class has a user-declared copy constructor
or a user-declared destructor.
The implicitly-declared copy assignment operator for a class
\tcode{X}
will have the form

\begin{codeblock}
X& X::operator=(const X&)
\end{codeblock}

if

\begin{itemize}
\item
each direct base class
\tcode{B}
of
\tcode{X}
has a copy assignment operator whose parameter is of type
\tcode{const}
\tcode{B\&},
\tcode{const}
\tcode{volatile}
\tcode{B\&}
or
\tcode{B},
and
\item
for all the non-static data members of
\tcode{X}
that are of a class type
\tcode{M}
(or array thereof),
each such class type has a copy assignment operator whose parameter is of type
\tcode{const}
\tcode{M\&},
\tcode{const}
\tcode{volatile}
\tcode{M\&}
or
\tcode{M}.\footnote{This implies that the reference parameter of the
implicitly-declared copy assignment operator cannot bind to a
\tcode{volatile}
lvalue; see~\ref{diff.special}.}
\end{itemize}

Otherwise, the implicitly-declared copy
assignment operator
will have the form

\begin{codeblock}
X& X::operator=(X&)
\end{codeblock}

\pnum
A user-declared move assignment operator \tcode{X::operator=} is
a non-static non-template member function of class \tcode{X} with exactly
one parameter of type \tcode{X\&\&}, \tcode{const X\&\&}, \tcode{volatile X\&\&}, or
\tcode{const volatile X\&\&}. \enternote An overloaded assignment operator must be
declared to have only one parameter; see~\ref{over.ass}. \exitnote{}
\enternote More
than one form of move assignment operator may be declared for a class. \exitnote

\pnum
\indextext{assignment operator!move!implicitly declared}%
If the definition of a class \tcode{X} does not explicitly declare a
move assignment operator, one
will be implicitly declared as defaulted if and only if

\begin{itemize}
\item
\tcode{X} does not have a user-declared copy constructor,

\item
\tcode{X} does not have a user-declared move constructor,

\item
\tcode{X} does not have a user-declared copy assignment operator, and

\item
\tcode{X} does not have a user-declared destructor.
\end{itemize}

\enterexample The class definition
\begin{codeblock}
struct S {
  int a;
  S& operator=(const S&) = default;
};
\end{codeblock}

will not have a default move assignment operator implicitly declared because the
copy assignment operator has been user-declared. The move assignment operator may
be explicitly defaulted.

\begin{codeblock}
struct S {
  int a;
  S& operator=(const S&) = default;
  S& operator=(S&&) = default;
};
\end{codeblock}
\exitexample

\pnum
The implicitly-declared move assignment operator for a class \tcode{X} will have the form
\begin{codeblock}
X& X::operator=(X&&);
\end{codeblock}

\pnum
The implicitly-declared copy/move assignment operator for class
\tcode{X}
has the return type
\tcode{X\&};
it returns the object for which the assignment operator is invoked, that is,
the object assigned to.
An implicitly-declared copy/move assignment operator is an
\tcode{inline}
\tcode{public}
member of its class.

\pnum
A defaulted copy/move assignment operator for
class \tcode{X} is defined as deleted if \tcode{X} has:
\begin{itemize}
\item a variant member with a non-trivial corresponding assignment operator and
  \tcode{X} is a union-like class, or

\item a non-static data member of \tcode{const} non-class
  type (or array thereof), or

\item a non-static data member of reference type, or

\item a potentially constructed subobject of class type \tcode{M}
  (or array thereof) that cannot be copied/moved because overload resolution
  (\ref{over.match}), as applied to \tcode{M}'s corresponding
  assignment operator, results in an ambiguity or
  a function that is deleted or inaccessible from the
  defaulted assignment operator.
\end{itemize}

A defaulted move assignment operator that is defined as deleted is ignored by
overload resolution~(\ref{over.match}, \ref{over.over}).

\pnum
\indextext{assignment operator!copy!hidden}%
\indextext{assignment operator!move!hidden}%
Because a copy/move assignment operator is implicitly declared for a class
if not declared by the user,
a base class copy/move assignment operator is always hidden
by the corresponding assignment operator of a derived class~(\ref{over.ass}).
A
\grammarterm{using-declaration}~(\ref{namespace.udecl}) that brings in from a base class an assignment operator
with a parameter type that could be that of a
copy/move assignment operator for the
derived class is not considered an explicit declaration of such an
operator and does not suppress the implicit declaration of the derived class
operator;
the operator introduced by the
\grammarterm{using-declaration}
is hidden by the implicitly-declared operator in the derived
class.

\pnum
\indextext{assignment operator!copy!trivial}%
\indextext{assignment operator!move!trivial}%
A copy/move assignment operator for class
\tcode{X}
is
trivial
if it is not user-provided, its parameter-type-list is equivalent to the parameter-type-list
of an implicit declaration, and if

\begin{itemize}
\item
class
\tcode{X}
has no virtual functions~(\ref{class.virtual})
and no virtual base classes~(\ref{class.mi}), and

\item class \tcode{X} has no non-static data members of volatile-qualified
type, and

\item the assignment operator selected to copy/move each direct
base class subobject is trivial, and

\item
for each non-static data member of
\tcode{X}
that is of class type (or array thereof),
the assignment operator selected to copy/move that member is trivial;
\end{itemize}

otherwise the copy/move assignment operator is
\grammarterm{non-trivial}.

\pnum
\indextext{assignment operator!copy!implicitly defined}%
\indextext{assignment operator!move!implicitly defined}%
A copy/move assignment operator for a class \tcode{X}
that is defaulted and not defined as deleted
is
\term{implicitly defined}
when
it is odr-used~(\ref{basic.def.odr}) (e.g., when it is selected by overload resolution
to assign to an object of its class type)
or when it is explicitly defaulted after its first declaration.
The implicitly-defined copy/move assignment operator is \tcode{constexpr} if
\begin{itemize}
\item
\tcode{X} is a literal type, and

\item
the assignment operator selected to copy/move each direct base class subobject
is a \tcode{constexpr} function, and

\item
for each non-static data member of \tcode{X} that is of class type (or array
thereof), the assignment operator selected to copy/move that member is a
\tcode{constexpr} function.
\end{itemize}

\pnum
Before the defaulted copy/move assignment operator for a class is
implicitly defined,
all non-user-provided copy/move assignment operators for
its direct base classes and
its non-static data members shall have been implicitly defined.
\enternote
An implicitly-declared copy/move assignment operator has an
\grammarterm{exception-specification}~(\ref{except.spec}).
\exitnote

\pnum
The implicitly-defined copy/move assignment operator for a
non-union class \tcode{X} performs memberwise copy/move assignment of its subobjects. The direct
base classes of \tcode{X} are assigned first, in the order of their declaration in the
\grammarterm{base-specifier-list}, and then the immediate non-static data members of
\tcode{X} are assigned, in the order in which they were declared in the class
definition.
Let \tcode{x} be either the parameter of the function or, for the move operator, an
xvalue referring to the parameter.
Each subobject is assigned in the manner appropriate to its type:

\begin{itemize}
\item
if the subobject is of class type,
as if by a call to \tcode{operator=} with the subobject as the object expression
and the corresponding subobject of \tcode{x} as a single function argument
(as if by explicit qualification; that is,
ignoring any possible virtual overriding functions in more derived classes);
\item
if the subobject is an array, each element is assigned,
in the manner appropriate to the element type;
\item
if the subobject is of scalar type,
the built-in assignment operator is used.
\end{itemize}

\indextext{assignment operator!copy!virtual bases and}%
It is unspecified whether subobjects representing virtual base classes
are assigned more than once by the implicitly-defined copy assignment
operator.
\enterexample

\begin{codeblock}
struct V { };
struct A : virtual V { };
struct B : virtual V { };
struct C : B, A { };
\end{codeblock}

It is unspecified whether the virtual base class subobject
\tcode{V}
is assigned twice by the implicitly-defined copy assignment operator for
\tcode{C}.
\exitexample
\enternote
\indextext{assignment operator!move!virtual bases and}%
This does not apply to move assignment, as a defaulted move assignment operator
is deleted if the class has virtual bases.
\exitnote

\pnum
The implicitly-defined copy assignment operator for a
union \tcode{X} copies the object representation~(\ref{basic.types}) of \tcode{X}.%
\indextext{assignment operator!move|)}%
\indextext{assignment operator!copy|)}

\pnum
\indextext{constructor!copy!inaccessible}%
\indextext{constructor!move!inaccessible}%
\indextext{assignment operator!copy!inaccessible}%
\indextext{assignment operator!move!inaccessible}%
A program is ill-formed if the copy/move constructor or the copy/move assignment
operator for an object is implicitly odr-used and the special member function
is not accessible (Clause~\ref{class.access}).
\enternote
Copying/moving one object into another using the copy/move constructor or
the copy/move assignment operator does not change the layout or size of either
object.
\exitnote

\pnum
\indextext{temporary!elimination~of}%
\indextext{elision!copy constructor|see{constructor, copy, elision}}%
\indextext{elision!move constructor|see{constructor, move, elision}}%
\indextext{constructor!copy!elision}%
\indextext{constructor!move!elision}%
When certain criteria are met, an implementation is
allowed to omit the copy/move construction of a class object,
even if the constructor selected for the copy/move operation and/or the
destructor for the object have
\indextext{side effects}%
side effects.  In such cases, the
implementation treats the source and target of the
omitted copy/move operation as simply two different ways of
referring to the same object, and the destruction of
that object occurs at the later of the times when the
two objects would have been destroyed without the
optimization.\footnote{Because only one object is destroyed instead of two,
and one copy/move constructor
is not executed, there is still one object destroyed for each one constructed.}
This elision of copy/move operations, called
\indexdefn{copy elision|see{constructor, copy, elision; constructor, move, elision}}%
\indexdefn{elision!copy|see{constructor, copy, elision; constructor, move, elision}}%
\indexdefn{constructor!copy!elision}\indexdefn{constructor!move!elision}\term{copy elision},
is permitted in the
following circumstances (which may be combined to
eliminate multiple copies):

\begin{itemize}
\item in a \tcode{return} statement in a function with a class return type,
when the expression is the name of a non-volatile
automatic object
(other than a function or catch-clause parameter)
with the same cv-unqualified type as
the function return type, the copy/move operation can be
omitted by constructing the automatic object directly
into the function's return value

\item in a \nonterminal{throw-expression}, when the operand
is the name of a non-volatile automatic object
(other than a function or catch-clause parameter)
whose scope does not extend beyond the end of the innermost enclosing
\grammarterm{try-block} (if there is one), the copy/move operation from the
operand to the exception object~(\ref{except.throw}) can be omitted by
constructing the automatic object directly into the exception object

\item when a temporary class object that has not been bound to a
reference~(\ref{class.temporary}) would be copied/moved to a class object with
the same cv-unqualified type, the copy/move operation can be omitted by constructing
the temporary object directly into the target of the omitted copy/move

\item when the \nonterminal{exception-declaration} of an
exception handler (Clause~\ref{except}) declares an object of the same
type (except for cv-qualification) as the exception
object~(\ref{except.throw}), the copy operation can be omitted by treating
the \nonterminal{exception-declaration} as an alias for the exception
object if the meaning of the program will be unchanged except for the execution
of constructors and destructors for the object declared by the
\nonterminal{exception-declaration}.
\enternote There cannot be a move from the exception object because it is
always an lvalue.  \exitnote
\end{itemize}

\enterexample

\begin{codeblock}
class Thing {
public:
  Thing();
  ~Thing();
  Thing(const Thing&);
};

Thing f() {
  Thing t;
  return t;
}

Thing t2 = f();
\end{codeblock}

Here the criteria for elision can be combined to
eliminate two calls to the copy constructor of class
\tcode{Thing}:
the copying of the local automatic object
\tcode{t}
into the temporary object for the return value of
function
\tcode{f()}
and the copying of that temporary object into object
\tcode{t2}.
Effectively, the construction of the local object
\tcode{t}
can be viewed as directly initializing the global
object
\tcode{t2},
and that object's destruction will occur at program
exit.
Adding a move constructor to \tcode{Thing} has the same effect, but it is the
move construction from the temporary object to \tcode{t2} that is elided.
\exitexample

\pnum
When the criteria for elision of a copy/move operation are met,
but not for an \nonterminal{exception-declaration},
and the object
to be copied is designated by an lvalue,
or when the \grammarterm{expression} in a \tcode{return} statement
is a (possibly parenthesized) \grammarterm{id-expression}
that names an object with automatic storage duration declared in the body
or \grammarterm{parameter-declaration-clause} of the innermost enclosing
function or \grammarterm{lambda-expression},
overload resolution to select the constructor
for the copy is first performed as if the object were designated by an rvalue.
If the first overload resolution fails or was not performed,
or if the type of the first parameter of the selected
constructor is not an rvalue reference to the object's type (possibly cv-qualified),
overload resolution is performed again, considering the object as an lvalue.
\enternote This two-stage overload resolution must be performed regardless
of whether copy elision will occur. It determines the constructor to be called if
elision is not performed, and the selected constructor must be accessible even if
the call is elided. \exitnote

\enterexample
\begin{codeblock}
class Thing {
public:
  Thing();
  ~Thing();
  Thing(Thing&&);
private:
  Thing(const Thing&);
};

Thing f(bool b) {
  Thing t;
  if (b)
    throw t;                    // OK: \tcode{Thing(Thing\&\&)} used (or elided) to throw t
  return t;                     // OK: \tcode{Thing(Thing\&\&)} used (or elided) to return t
}

Thing t2 = f(false);            // OK: \tcode{Thing(Thing\&\&)} used (or elided) to construct t2
\end{codeblock}
\exitexample

\rSec1[class.inhctor]{Inheriting constructors}%
\indextext{constructor!inheriting|(}

\pnum
A \grammarterm{using-declaration}~(\ref{namespace.udecl}) that names a
constructor implicitly declares a set of \term{inheriting constructors}.
The \term{candidate set of inherited constructors} from the class \tcode{X}
named in the \grammarterm{using-declaration} consists of actual constructors and
notional constructors that result from the transformation of defaulted
parameters as follows:

\begin{itemize}
\item all non-template constructors of \tcode{X}, and
\item for each non-template constructor of \tcode{X} that has at least one parameter with a default argument, the set of constructors that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list, and
\item all constructor templates of \tcode{X}, and
\item for each constructor template of \tcode{X} that has at least one parameter with a default argument, the set of constructor templates that results from omitting any ellipsis parameter specification and successively omitting parameters with a default argument from the end of the parameter-type-list.
\end{itemize}

\pnum
The \term{constructor characteristics} of a constructor or constructor template are

\begin{itemize}
\item the template parameter list~(\ref{temp.param}), if any,
\item the \grammarterm{parameter-type-list}~(\ref{dcl.fct}),
\item absence or presence of \tcode{explicit}~(\ref{class.conv.ctor}), and
\item absence or presence of \tcode{constexpr}~(\ref{dcl.constexpr}).
\end{itemize}

\pnum
For each non-template constructor in the candidate set of inherited constructors other
than a constructor having no parameters or a copy/move constructor having a single parameter,
a constructor is implicitly declared with the same constructor characteristics unless
there is a user-declared constructor with the same signature in the complete class where the
\grammarterm{using-declaration} appears or the constructor would be a default, copy, or move
constructor for that class. Similarly, for each constructor template in the
candidate set of inherited constructors, a constructor template is implicitly declared
with the same constructor characteristics unless there is an equivalent user-declared
constructor template~(\ref{temp.over.link}) in the complete class where the \grammarterm{using-declaration}
appears. \enternote Default arguments are not inherited. An
\grammarterm{exception-specification} is implied as specified
in~\ref{except.spec}.\exitnote

\pnum
A constructor so declared has the same access as the corresponding constructor
in \tcode{X}. It is deleted if the corresponding constructor in \tcode{X} is
deleted~(\ref{dcl.fct.def}). An inheriting constructor shall not be explicitly 
instantiated~(\ref{temp.explicit}) or explicitly specialized~(\ref{temp.expl.spec}).

\pnum
\enternote Default and copy/move constructors may be implicitly declared as specified in~\ref{class.ctor} and~\ref{class.copy}. \exitnote

\pnum \enterexample

\begin{codeblock}
struct B1 {
  B1(int);
};

struct B2 {
  B2(int = 13, int = 42);
};

struct D1 : B1 {
  using B1::B1;
};

struct D2 : B2 {
  using B2::B2;
};
\end{codeblock}

The candidate set of inherited constructors in \tcode{D1} for \tcode{B1} is

\begin{itemize}
\item \tcode{B1(const B1\&)}
\item \tcode{B1(B1\&\&)}
\item \tcode{B1(int)}
\end{itemize}

The set of constructors present in \tcode{D1} is

\begin{itemize}
\item \tcode{D1()}, implicitly-declared default constructor, ill-formed if odr-used
\item \tcode{D1(const D1\&)}, implicitly-declared copy constructor, not inherited
\item \tcode{D1(D1\&\&)}, implicitly-declared move constructor, not inherited
\item \tcode{D1(int)}, implicitly-declared inheriting constructor
\end{itemize}

The candidate set of inherited constructors in \tcode{D2} for \tcode{B2} is

\begin{itemize}
\item \tcode{B2(const B2\&)}
\item \tcode{B2(B2\&\&)}
\item \tcode{B2(int = 13, int = 42)}
\item \tcode{B2(int = 13)}
\item \tcode{B2()}
\end{itemize}

The set of constructors present in \tcode{D2} is

\begin{itemize}
\item \tcode{D2()}, implicitly-declared default constructor, not inherited
\item \tcode{D2(const D2\&)}, implicitly-declared copy constructor, not inherited
\item \tcode{D2(D2\&\&)}, implicitly-declared move constructor, not inherited
\item \tcode{D2(int, int)}, implicitly-declared inheriting constructor
\item \tcode{D2(int)}, implicitly-declared inheriting constructor
\end{itemize}
\exitexample

\pnum
\enternote If two \grammarterm{using-declaration}{s} declare inheriting constructors with the same
signatures, the program is ill-formed~(\ref{class.mem}, \ref{over.load}), because an
implicitly-declared constructor introduced by the first \grammarterm{using-declaration} is not a
user-declared constructor and thus does not preclude another declaration of a constructor with the
same signature by a subsequent \grammarterm{using-declaration}. \enterexample

\begin{codeblock}
struct B1 {
  B1(int);
};

struct B2 {
  B2(int);
};

struct D1 : B1, B2 {
  using B1::B1;
  using B2::B2;     
};                  // ill-formed: attempts to declare D1(int) twice

struct D2 : B1, B2 {
  using B1::B1;
  using B2::B2;
  D2(int);          // OK: user declaration supersedes both implicit declarations
};
\end{codeblock}
\exitexample \exitnote

\pnum
An inheriting constructor for a class is implicitly defined when
it is odr-used~(\ref{basic.def.odr}) to
create an object of its class type~(\ref{intro.object}). An implicitly-defined inheriting
constructor performs the set of initializations of the class that would be performed by a
user-written \tcode{inline} constructor for that class with a \grammarterm{mem-initializer-list}
whose only \grammarterm{mem-initializer} has a \grammarterm{mem-initializer-id} that names the base
class denoted in the \grammarterm{nested-name-specifier} of the
\grammarterm{using-declaration} and an \grammarterm{expression-list} as specified below, and where
the \grammarterm{compound-statement} in its function body is empty~(\ref{class.base.init}). If that
user-written constructor would be ill-formed, the program is ill-formed. Each
\grammarterm{expression} in the \grammarterm{expression-list} is of the form
\tcode{static_cast<T\&\&>(p)}, where \tcode{p} is the name of the corresponding constructor
parameter and \tcode{T} is the declared type of \tcode{p}.

\pnum
\enterexample

\begin{codeblock}
struct B1 {
  B1(int) { }
};

struct B2 {
  B2(double) { }
};

struct D1 : B1 {
  using B1::B1;     // implicitly declares \tcode{D1(int)}
  int x;
};

void test() {
  D1 d(6);          // OK: \tcode{d.x} is not initialized
  D1 e;             // error: \tcode{D1} has no default constructor
}

struct D2 : B2 {
  using B2::B2;     // OK: implicitly declares \tcode{D2(double)}
  B1 b;
};

D2 f(1.0);          // error: B1 has no default constructor

template< class T >
struct D : T {
  using T::T;       // declares all constructors from class T
  ~D() { std::clog << "Destroying wrapper" << std::endl; }
};
\end{codeblock}

Class template \tcode{D} wraps any class and forwards all of its constructors,
while writing a message to the standard log whenever an object of class
\tcode{D} is destroyed. \exitexample%
\indextext{constructor!inheriting|)}
