Suppose I create an instance 'obj' of the 'Test' class:
Test obj;
Now, suppose I write
Test another = obj;
My question: is this last line called 'direct -initialization' or
'copy-initialization' ?
Kindly clarify.
Thanks
V.Subramanian
Hi
AFAIK, there isn't "direct -initialization" terminology in standard C+
+. I think the term "copy-initialization"
isn't standard too. What is clear is, in the following statements:
Test obj;
Test another = obj;
first an object (obj) is created using default constructor, then
another object (another) is created using
copy constructor. The second one is called "initialization", because
another is initialized using obj.
Cheers,
Saeed Amrollahi
It's copy-initialization, but since obj is the same type as another,
it has the semantics of direct-initialization (that is, it constructs
another using Test's copy constructor, without any temporary). See
section 8.5 paragraphs 12 and 14 (shown below).
ISO/IEC 14882:2003
8.5 Initializers
[...]
11 The form of initialization (using parentheses or =) is generally
insignificant, but does matter when the entity being initialized has a
class type; see below. A parenthesized initializer can be a list of
expressions only when the entity being initialized has a class type.
12 The initialization that occurs in argument passing, function return,
throwing an exception (15.1), handling an exception (15.3), and
brace-enclosed initializer lists (8.5.1) is called copy-initialization
and is equivalent to the form
T x = a;
The initialization that occurs in new expressions (5.3.4), static_cast
expressions (5.2.9), functional notation type conversions (5.2.3), and
base and member initializers (12.6.2) is called direct-initialization
and is equivalent to the form
T x(a);
[...]
14 The semantics of initializers are as follows.
[...]
‹ If the destination type is a (possibly cv-qualified) class type:
‹ If the class is an aggregate (8.5.1), and the initializer is a
brace-enclosed list, see 8.5.1.
‹ If the initialization is direct-initialization, or if it is
copy-initialization where the cv-unqualified version of the source
type is the same class as, or a derived class of, the class of the
destination, constructors are considered. The applicable
constructors are enumerated (13.3.1.3), and the best one is chosen
through overload resolution (13.3). The constructor so selected is
called to initialize the object, with the initializer
expression(s) as its argument(s). If no constructor applies, or
the overload resolution is ambiguous, the initialization is
ill-formed.
‹ Otherwise (i.e., for the remaining copy-initialization cases),
user-defined conversion sequences that can convert from the source
type to the destination type or (when a conversion function is
used) to a derived class thereof are enumerated as described in
13.3.1.4, and the best one is chosen through overload resolution
(13.3). If the conversion cannot be done or is ambiguous, the
initialization is ill-formed. The function selected is called with
the initializer expression as its argument; if the function is a
constructor, the call initializes a temporary of the destination
type. The result of the call (which is the temporary for the
constructor case) is then used to direct-initialize, according to
the rules above, the object that is the destination of the
copy-initialization. In certain cases, an implementation is
permitted to eliminate the copying inherent in this
direct-initialization by constructing the intermediate result
directly into the object being initialized; see 12.2, 12.8.
Hi
I am sorry for some inaccuracy. My answer is not incorrect in general,
but I read the
C++ standard draft ISO/IEC JTC 1/SC 22 N 4411 and I found the
following statements:
The initialization that occurs in the form
T x = a;
as well as in argument passing, function return, throwing an
exception, handling an exception,
and aggregate member initialization is called copy-initialization.
The initialization that occurs in in the forms
T x(a);
T x{a};
as well as in new expressions, static_cast expressions, functional
notation type conversions, and base and
member initializers is called direct-initialization.
So I think the statement:
Test another = obj;
should be copy-initialization.
Cheers,
Saeed Amrollahi