* (e-mail address removed), India:
By definition. The term refers to the syntax.
This is called direct initialization (syntax).
Not for the example you've shown. But in other cases yes. E.g.
may create a temporary Test and copy that, while
May, but it isn't required to, and none of the compilers I know
do in fact create the temporary. (It is required that their be
an accessible copy constructor, however, even if it isn't used.)
I generally prefer the "=" because it's more clear.
Yes. It makes it clear that there is an assignment (that the
operator=) is used
.
Seriously, I don't think that there is a "clearer" solution.
Both have problems. I generally use copy-initialization when
non-class types are involved, direct-initialization otherwise.
For the novice it also avoids the dreaded so called "most
vexing parse" where the compiler treats an attempted variable
declaration as a routine declaration.
But in some cases you can't use "=", because it requires an
accessible copy constructor. For example, you can't initialize
a std:
stringstream that way, since streams are not copyable.
In a lot of cases, you can't use the copy-initialization format,
because it only allows a single argument (unless you explicitly
construct on the right side of the =). I use
direct-initialization for class types because the syntax reminds
me of calling the constructor (which is what actually happens).
I use copy-initialization for other types because that's what
I'm used to, going back to my C programming days. And because
at least one early C++ compiler choked on direct-initialization
for some basic types. (But those are very much purely personal
preferences.)
Conversely, in a great many cases you can't use direct
initialization syntax, e.g. it's not supported for
specififying a default for a formal argument.
That's a different case---you're specifying a default argument
for a single parameter, not for the object. It's not
"initialization", at least not in the sense the standard uses
the term.
So it's not possible to choose a single syntax to use
everywhere.
Yes and no. If you always enclose any arguments in an
additional set of parentheses (so they can't be interpreted as
parameters of a function), then you can use
direct-initialization for most types. But not for aggregate
initialization, e.g.:
int array[] = { 1, 2, 5 } ;
At least until C++0x (but I don't think even C++0x will make
that possible).
C++0x adds braced-list-initialization, which is also
direct-initialization (unless there is an = sign before the
braced list, in which case, it is copy initialization), but with
a syntax which should avoid the most difficult parse problem.
But I'm not sure that it can always be used:
std::vector< size_t > v1( 42, 1 ) ;
std::vector< size_t > v2{ 42, 1 } ;
The first creates a vector with 42 elements, all initialized
with 1. IIUC, the second creates a vector with two elements,
the first of which is 42, and the second 1. Other than writing
out 42 1's, I don't think that there's any way of getting the
effect of the first constructor with list initialization.