static_casts

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

Rob Williscroft

Richard Hayden wrote in in
comp.lang.c++:
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:

Which version of g++ ?, I tried 3.2 and 3.4 and it worked fine,
I also tried msvc 7.1 and CBuilderX and they worked fine too.


Rob.
 
R

Richard Hayden

Rob said:
Richard Hayden wrote in in
comp.lang.c++:




Which version of g++ ?, I tried 3.2 and 3.4 and it worked fine,
I also tried msvc 7.1 and CBuilderX and they worked fine too.


Rob.

I'm using version 3.3.1.

I get this error on trying to compile the static_cast<C3>(c1obj) line:

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

Surely this cast *shouldn't* work though? But then there is surely a
contradiction with the standard because the aforementioned other direct
initialisation does compile?

Regards,

Richard Hayden.
 
R

Rob Williscroft

Richard Hayden wrote in in
comp.lang.c++:
I'm using version 3.3.1.

I get this error on trying to compile the static_cast<C3>(c1obj) line:

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

Surely this cast *shouldn't* work though?

Well the standard, as you've quoted, say's otherwise.

If you think this is a defect you should maybe ask in comp.std.c++.
But then there is surely a
contradiction with the standard because the aforementioned other direct
initialisation does compile?

FWIW, here is the programme I compiled/ran with the 4 compilers above:

#include <iostream>
#include <ostream>

class C1 {};

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

class C3 {
public:
C3(C2) {}
int f() { return 0; }
};

C1 c1obj;
C3 c3obj(c1obj);

int main()
{
C3 c3 = static_cast<C3>(c1obj);
std::cout << "Ok" << std::endl;;

return c3.f();
}

Rob.
 
A

Andrey Tarasevich

Richard said:
...
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).
...

Why? Only one _implicit_ user-defined conversion is required here (C1 ->
C2). As the standard says (an× you quoted it)

static_cast<C3>(c1obj)

is equivalent to

C3 t(C1)

i.e. it is equivalent to the above direct-initialization version. Both
should compile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top