R
Richard Hayden
Hi,
Given the following code:
/**************************/
class C1 {};
class C2 {
public:
C2(C1) {}
};
class C3 {
public:
C3(C2) {}
};
/**************************/
This:
C1 c1obj;
C3 c3obj(c1obj);
works fine (in g++).
However this:
C1 c1obj;
static_cast<C3>(c1obj);
doesn't (as you would expect as it requires two user-defined
conversions). But this seems 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."
What have I missed?
Thanks,
Richard Hayden.
Given the following code:
/**************************/
class C1 {};
class C2 {
public:
C2(C1) {}
};
class C3 {
public:
C3(C2) {}
};
/**************************/
This:
C1 c1obj;
C3 c3obj(c1obj);
works fine (in g++).
However this:
C1 c1obj;
static_cast<C3>(c1obj);
doesn't (as you would expect as it requires two user-defined
conversions). But this seems 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."
What have I missed?
Thanks,
Richard Hayden.