Topic: A C++ standard question. (defect in standard?)
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/10/22 Raw View
On 21 Oct 99 09:41:45 GMT, Salters <salters@lucent.com> wrote:
: TiTi wrote:
: > " An expression e:
[snip]
: > I always thought that "T t=e;" is equivalent to "T t(e)". Then why
: > does the
: > standard use one version in [4], and the other in [5.2.9]? Are there
: > differences I am not aware of?
: The obvious:
:
: T t=e requires an accessible T::operator=([const] E& e),
Sorry, this is an initialization which has nothing to do with
operator= which is used in assignments.
: while
: T t(e) requires an accessible T::T([const] E& e). Note const is
: optional, not literally enclosed in suare brackets.
If the expression e is not of type T, T t = e; is T t(T(e)); which
requires a conversion from E to T, either T::T(E) or E::operator T,
and a copy ctor for T which are accessible. Note that the T(e) is
implicit which also requires that it not be explicit.
John
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Salters <salters@lucent.com>
Date: 1999/10/21 Raw View
TiTi wrote:
=
> >From the ISO/IEC standard
=
> [4 subnote3]
=
> " An expression e can be implicitly converted to a type T if and only i=
f the
> declaration "T t=3De;" is well=ADformed,
> for some invented temporary variable t (8.5). The effect of the implici=
t
> conversion is the same as
> performing the declaration and initialization and then using the tempor=
ary
> variable as the result of the con=ADversion.
> The result is an lvalue if T is a reference type (8.3.2), and an rvalue=
> otherwise. The expression e
> is used as an lvalue if and only if the initialization uses it as an
> lvalue."
=
> and
=
> [5.2.9 subnote2]
=
> "An expression e can be explicitly converted to a type T using a static=
_cast
> of the form
> static_cast<T>(e) if the declaration "T t(e);" is well=ADformed, for so=
me
> invented temporary vari=ADable
> t (8.5). The effect of such an explicit conversion is the same as perfo=
rming
> the declaration and initial=ADization
> and then using the temporary variable as the result of the conversion. =
The
> result is an lvalue if T is a
> reference type (8.3.2), and an rvalue otherwise. The expression e is us=
ed as
> an lvalue if and only if the
> initialization uses it as an lvalue."
> I always thought that "T t=3De;" is equivalent to "T t(e)". Then why do=
es the
> standard use one version in [4], and the other in [5.2.9]? Are there
> differences I am not aware of?
> TiTi
The obvious:
T t=3De requires an accessible T::operator=3D([const] E& e), while
T t(e) requires an accessible T::T([const] E& e). Note const is
optional, not literally enclosed in suare brackets.
This matters if e.g. one of these is public, while the other is private.
I do wonder if these statement are correct, though. If e is a member
of class E, and E has E::operator T(), e can be converted to T.
This can happen even if T's assignment operator and constructor
are both private ( E likely a friend of T ), and T t(e) as well as
T t=3De would not be well-formed, right ?
This is covered by [4, subnote 4] (user defined conversions)
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Darin Adler <darin@bentspoon.com>
Date: 1999/10/21 Raw View
Salters <salters@lucent.com> wrote:
> The obvious:
>
> T t=e requires an accessible T::operator=([const] E& e), while
> T t(e) requires an accessible T::T([const] E& e). Note const is
> optional, not literally enclosed in suare brackets.
>
> This matters if e.g. one of these is public, while the other is private.
I believe that's wrong.
"T t=e;" requires an accessible "T::T([const] E&)" too, but it must not be
declared with "explicit". "T t=e;" is a definition, not an assignment,
believe it or not, despite the presence of an "=" sign.
The assignment operator would be used for "T t; t=e;".
-- Darin
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/10/21 Raw View
Salters wrote:
>
>
> T t=e requires an accessible T::operator=([const] E& e), while
> T t(e) requires an accessible T::T([const] E& e). Note const is
> optional, not literally enclosed in suare brackets.
T t = e does not REQUIRE an operator=.
However there are differences between direct (T t(e)) and copy (T t=e)
initialization.
The difference is the way conversions happen when e is not the same
type as T (or a suitably dervied type).
In direct initialization, only the constructors are considered.
In copy initialization, user defined conversions are possible to
convert e to a temporary T (or a suitably derived type) that is then
used to direct initialize t.
In neither case is operator= an essential player.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]