473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

static_casts

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
4 1454
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Wagner | last post by:
I do some Windows kernel programming, where what I need to pass to some Kernel call is "void* Context". Sometime later, I will get that Conext back. I want to pass a class pointer to this system...
5
by: vw at iep dot tu-graz dot ac dot at | last post by:
Hello Is there something more elegant in C++ to read/write data binary into a stream than for example: out.write(reinterpret_cast<const char *>(&long_var), sizeof(long_var));...
15
by: iuweriur | last post by:
A few questions on the curiously recurring template pattern: This page: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate this part: template<typename T> struct ArithmeticType { T operator...
6
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
26
by: LuB | last post by:
This isn't a C++ question per se ... but rather, I'm posting this bcs I want the answer from a C++ language perspective. Hope that makes sense. I was reading Peter van der Linden's "Expert C...
8
by: Adam | last post by:
Is there a difference between the expressions "size_t(-1)" and "(size_t)-1"? In context, say: void Some_Function(size_t); Some_Function(size_t(-1)); //pass a flag value to Some_Function
4
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.