Topic: A C++ standard question. (Implicit conversion, defect in standard?)


Author: Salters <salters@lucent.com>
Date: 1999/10/25
Raw View
Salters wrote:

> TiTi wrote:

> > >From the ISO/IEC standard

> > [4  subnote3]

> > " An expression e can be implicitly converted to a type T if and only=
 if the
> > declaration "T t=3De;" is well=ADformed,
> > for some invented temporary variable t (8.5). The effect of the impli=
cit
> > conversion is the same as
> > performing the declaration and initialization and then using the temp=
orary
> > variable as the result of the con=ADversion.
> > The result is an lvalue if T is a reference type (8.3.2), and an rval=
ue
> > otherwise. The expression e
> > is used as an lvalue if and only if the initialization uses it as an
> > lvalue."

> > and

[ equivalence of explicit conversion to T t(e); ]

> > I always thought that "T t=3De;" 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?

> > 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.

> 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)

Reminder to self: Don't post before first cup of coffee.

Of course, everybody responded (correctly) that T::operator=3D() is never=

needed.

I still wonder about the equivalence, though.

T t=3De indeed exists iff ( e can be implicitly converted to a T,
either by virtue of T::T(E const&) or E::operator T(), and the
appropriate constructor is accessible)

The appropriate constructor is T::T(E const&) in the case that
is used for the implicit conversion, or T::T(T const&) in case
the implicit conversion is E::operator T. [Note: T::T(E const &)
is not used for an implicit conversion if it is explicit].

// Now, let T be =

class T
{
 private:
 T() {;}
 T(E const& e) {;}
 friend class E;
}
// and let E be
class E
{
 public:
 T operator T() { return T(); }
 void f(T& t){;}
}
// Create an e
E e;
// Call a function requiring an implicit conversion to T
e.f(e); =

// Cannot write T t=3De;
#ifdef BROKEN
T t=3De;
#endif

The call to E::f should be illegal per the standard, but
is this really read so?
---
[ 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              ]