On Nov 25, 3:14 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Quote:
Suppose we have a class named Test.
Quote:
Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization happens here
Test copy_init = obj; // copy initialization happens here
Quote:
Suppose we have a function
Quote:
void fn(Test arg);
Quote:
When we call fn() with fn(obj), the copy ctor
is called to construct 'arg'. Am I correct?
Yes.
Quote:
Question is what kind of initialization - direct
initialization or copy initialization, happens to
construct arg ?
Formally, it's copy initialization. But the standard also says
that if the type of the initialization is the same as, or a
class derived from, the target type, copy initialization behaves
the same as direct initialization. There is only a difference
when conversions are involved.
Quote:
What does the standard say in this regard ?
Exactly what I just said.
Quote:
If copy initialization happens, then if the copy
ctor is 'explicit', then calling fn(obj) will not
compile. Am I correct ?
I don't think so, but the original standard is far from clear.
In the section on initialization, it makes it very clear that if
the initialization expression in copy initialization has the
same type as, or a type derived from, the target type, the
semantics are the same as those of direct initialization. In
the section concerning explicit, on the other hand, the latest
draft says that an explicit constructor will only be used "where
the direct-initialization syntax (8.5) or where casts (5.2.9,
5.4) are explicitly used." (Of course, it contradicts this for
explicit default constructors in the very next sentence. But
that doesn't affect the copy constructor.)
The simplest solution is just to never declare the copy
constructor explicit. (Since explicit is designed to prevent
conversions, and the copy constructor doesn't "convert" anything
in the classical sense of the word, the logical decision would
have been for explicit to be ignored on the copy constructor.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34