Topic: A C++ standard question.
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/27 Raw View
Salters wrote:
> // 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);
This is ill-formed, as f takes a non const reference,
and the result of a conversion is a rvalue (temporary
object).
--
Valentin Bonnard
---
[ 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: Michiel Salters <salters@lucent.com>
Date: 1999/10/27 Raw View
Valentin Bonnard wrote:
>
> Salters wrote:
>
> > // 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);
>
> This is ill-formed, as f takes a non const reference,
> and the result of a conversion is a rvalue (temporary
> object).
Of course you're right - I hacked up a function I thought
would cause an implicit conversion. Please read
void f(const T& t) const {;}
(Really, we don't change anything :)
This requires an implicit conversion, the only one that is
selected is E::operator T, but the standard seems to forbid
this (I think T t=e; is not well-formed.)
Michiel Salters
---
[ 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: "Bill Wade" <bill.wade@stoner.com>
Date: 1999/10/24 Raw View
TiTi wrote in message <7ufaau$q7s$1@trex.antw.online.be>...
>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?
In cases where T is a class and e is not T (or derived from T):
1. Copy initialization (the form with the '=' sign) won't use 'explicit'
constructors. Direct initialization (the form with the parenthesis) will
use explicit constructors.
2. Copy initialization won't work if certain types of intermediate
conversions are necessary.
3. Copy initialization requires something that looks a lot like a copy
constructor to be accessible.
4. Copy initialization might cause a copy constructor to be called. I think
compilers are almost always allowed to skip this (even if side effects are
involved), but I don't think they are ever required to skip it.
There may be situations where copy initialization succeeds, because it is
unambiguous and direct initialization fails because it is ambiguous. I'll
pose the question in a new thread.
---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/10/21 Raw View
On 21 Oct 99 06:40:04 GMT, "TiTi" <TJunkMyAss@mad.scientist.com> wrote:
: 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?
struct X {
explicit X (int);
}
void f (X);
void g () {
X x1(5); // Well formed
X x2 = 5; // Ill formed
f(5); // Ill formed
f(static_cast<X>(5)); // Well formed
}
See 8.5/12-14 for differences.
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: Darin Adler <darin@bentspoon.com>
Date: 1999/10/21 Raw View
TiTi <TJunkMyAss@mad.scientist.com> wrote:
> 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 difference I'm aware of is that in the presence of an explicit
constructor for class T, "T t=e;" will result in a diagnostic but "T t(e);"
will work without complaint.
-- 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: "TiTi" <TJunkMyAss@mad.scientist.com>
Date: 1999/10/21 Raw View
>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=e;" is well formed,
for some invented temporary variable t (8.5). The effect of the implicit
conversion is the same as
performing the declaration and initialization and then using the temporary
variable as the result of the con version.
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 formed, for some
invented temporary vari able
t (8.5). The effect of such an explicit conversion is the same as performing
the declaration and initial ization
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 used as
an lvalue if and only if the
initialization uses it as an lvalue."
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?
TiTi
---
[ 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 ]