Topic: Why is implicit conversion defined in terms of copy-initialization?


Author: scott douglass <sdouglass@arm.com>
Date: 2000/11/09
Raw View
In 4/3 and 4/4 the standard says:
 >>
An expression e can be implicitly converted to a type T if and only if th=
e=20
declaration "T t =3D e;" is well=ADformed, for some invented temporary va=
riable=20
t (8.5). The effect of the implicit conversion is the same as performing=20
the declaration and initialization and then using the temporary variable =
as=20
the result of the conversion. The result is an lvalue if T is a reference=
=20
type (8.3.2), and an rvalue otherwise. The expression e is used as an=20
lvalue if and only if the initialization uses it as an lvalue.

[Note: For user-defined types, user-defined conversions are considered as=
=20
well; see 12.3. In general, an implicit conversion sequence (13.3.3.1)=20
consists of a standard conversion sequence followed by a user=ADdefined=20
conversion followed by another standard conversion sequence.

[...]
<<

Why does the first sentence use copy-initialization, i.e. "T t =3D e;",=20
instead of direct-initialization, i.e. "T t(e);"?  I would have expected=20
that implicit conversion would be defined in terms of direct-initializati=
on.

For built-in types copy-initialization and direct-initialization are=20
equivalent.

For user-defined types, in certain cases, copy-initialization is equivale=
nt=20
to a direct-initialization followed by a copy-construction.  If the copy=20
constructor is inaccessible then the copy-initialization, and hence, the=20
implicit conversion is ill-formed.

One possible reason that 4/3 uses "T t =3D e;" might be that 4/3 is only=20
talking about built-in types.  The presence of the note tends to support=20
this but the note is, of course, non-normative.

Wouldn't it be better if 4/3 said "T t(e);" instead of "T t =3D e;"?  The=
n=20
4/3 would be apply for both built-in and user-defined types.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]