static_cast

static_cast

Post by rah0 » Mon, 23 Aug 2004 03:19:26


Hi,

Given the following code:

/**************************/

class C1 {};

class C2 {
public:
C2(C1) {}
};

class C3 {
public:
C3(C2) {}
};

/**************************/

This (assuming everything is in scope etc.):

C1 c1obj;
C3 c3obj(c1obj);

works fine in g++ version 3.3.1 (as you would imagine).

However this:

C1 c1obj;
static_cast<C3>(c1obj);

doesn't (as I expected since it requires two user-defined conversions to
convert from C1 to C3). g++ says 'invalid static_cast from C1 to C3'.
But this is in contradiction to the standard, which says:

5.2.9/2:

"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
wellformed, for some invented temporary variable t (8.5). The effect of
such an explicit conversion is the same as performing the declaration
and initialization 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."

Am I correct in saying that g++ version 3.3.1 does not obey the standard
in this case and thus that static_cast when implemented correctly can
cast between types where there are two levels of user-defined
conversions required? If so, I ask why 2 as opposed to 1? Have I missed
something?

Thanks,

Richard Hayden.

---
[ 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.yqcomputer.com/ ]
 
 
 

static_cast

Post by gi2nospa » Mon, 23 Aug 2004 04:46:36


..

FYI - gcc 3.2.2 and gcc 3.4.0 accepts your code.

.. slightly modified below.

/**************************/

class C1 {};

class C2 {
public:
C2(C1) {}
};

class C3 {
public:
C3(C2) {}
};

/**************************/


int main()
{

C1 c1obj;
C3 c3obj(c1obj);


c3obj = static_cast<C3>(c1obj);
}

---
[ 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.yqcomputer.com/ ]

 
 
 

static_cast

Post by rah0 » Mon, 23 Aug 2004 07:19:19


OK, I've verified that the exact code you've supplied above returns the
following error in g++ 3.3.1:

error: invalid static_cast from type `C1' to type `C3'

So I assume this an error on the part of g++?

Furthermore, why does static_cast allow conversions which require two
consecutive user-defined conversions? This just doesn't seem right to me
since all of the overloading documentation discusses implicit conversion
sequences as consisting of a standard conversion followed by one user
defined conversion followed by a standard conversion. It seems strange
that static_cast should be any different?

Thanks,

Richard Hayden.

---
[ 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.yqcomputer.com/ ]