By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

static_casts

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Richard Hayden wrote in news:cg**********@hercules.btinternet.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
Rob Williscroft wrote:
Richard Hayden wrote in news:cg**********@hercules.btinternet.com 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.


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.
Jul 22 '05 #3

P: n/a
Richard Hayden wrote in news:cg**********@titan.btinternet.com in
comp.lang.c++:
Rob Williscroft wrote:
Richard Hayden wrote in news:cg**********@hercules.btinternet.com 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.


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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
Richard Hayden wrote:
...
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.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.