static_cast<unsigned short&>(long_type): 4 different compilers - 3different behaviors for that state

A

Alex Vinokur

Hi,

static_cast<unsigned short&>(long_type);


4 different compilers - 3 different behaviors for that statement.



Regards,

Alex Vinokur



// ============ cast_test.cpp (BEGIN) ============
#include <cstdlib>
#include <iostream>
#include <iomanip>


bool isBigEndian()
{
unsigned short sh = 1;

char* ch = reinterpret_cast<char*>(&sh);

return (0 == *ch);
}

// ---------------
void showCompiler()
{
#if defined __hpux
system ("aCC -V");
#elif defined _AIX
system ("xlC -qversion");
#elif defined __sun
system ("CC -V");
#elif (defined __linux && defined __INTEL_COMPILER)
system (" icpc -V");
#else
#error Undefined platform
#endif
}

// ---------------
void showEnv()
{
std::cout << std::endl;
system ("uname -a");

std::cout << std::endl;
showCompiler();
std::cout << std::endl;

std::cout << "sizeof(long) = " << sizeof(long) << std::endl;
std::cout << std::endl;

std::cout << "ENDIAN: " << (isBigEndian() ? "BIG" : "LITTLE") <<
std::endl;
std::cout << std::endl;
}


// -------------
void castTest()
{

#define SHOW(x) std::cout << std::setw(2) << std::left << #x <<
std::right << " = " << std::hex << std::showbase << x << std::dec <<
std::endl

unsigned long l = 0x13579bdf2458ace0;
unsigned short s1 = static_cast<unsigned short>(l);

unsigned short s2 = static_cast<const unsigned short&>(l);
unsigned short s3 = static_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));

unsigned short s4 = reinterpret_cast<const unsigned short&>(l);
unsigned short s5 = reinterpret_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));

SHOW(l);
SHOW(s1);
SHOW(s2);
SHOW(s3);
SHOW(s4);
SHOW(s5);

}


int main()
{
showEnv();
castTest();

return 0;
}


// ============ cast_test.cpp (END) ============



Compilation and running


// ====== cast test on HP-UX ======
aCC +DD64 -AA cast_test.cpp
// No errors


HP-UX hpx418 B.11.23 U ia64 1139467043 unlimited-user license

aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0xace0
s3 = 0xace0
s4 = 0x1357
s5 = 0x1357


// ====== cast test on SUN ======
CC -m64 cast_test.cpp
// No errors


SunOS ilsun015 5.10 Generic_139555-08 sun4u sparc SUNW,SPARC-
Enterprise

CC: Sun C++ 5.10 SunOS_sparc 128228-08 2010/04/21
Usage: CC [ options ] files. Use 'CC -flags' for details

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0x1357
s3 = 0x1357
s4 = 0x1357
s5 = 0x1357


// ====== cast test on AIX ======
xlC -q64 -qwarn64 cast_test.cpp
// No errors

AIX ep5512b 1 6 000497A2D900

IBM XL C/C++ for AIX, V10.1
Version: 10.01.0000.0000

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0xace0
s3 = 0xace0
s4 = 0x1357
s5 = 0x1357


// ====== cast test on Linix & INTEL-compiler ======

Linux illin025 2.6.18-92.1.22.el5 #1 SMP Fri Dec 5 09:28:22 EST 2008
x86_64 x86_64 x86_64 GNU/Linux

Intel(R) C++ Intel(R) 64 Compiler Professional for applications
running on Intel(R) 64, Version 11.0 Build 20090318 Package ID:
l_cproc_p_11.0.083
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.
icpc cast_test.cpp

cast_test.cpp(58): error: invalid type conversion: "unsigned long *"
to "const unsigned short &"
unsigned short s2 = static_cast<const unsigned short&>(l);
^

cast_test.cpp(59): error: invalid type conversion: "const unsigned
long *" to "const unsigned short &"
unsigned short s3 = static_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));
^

compilation aborted for cast_test.cpp (code 2)
 
A

Alf P. Steinbach /Usenet

* Alex Vinokur, on 18.03.2011 06:11:
Hi,

static_cast<unsigned short&>(long_type);


4 different compilers - 3 different behaviors for that statement.



Regards,

Alex Vinokur



// ============ cast_test.cpp (BEGIN) ============
#include<cstdlib>
#include<iostream>
#include<iomanip>


bool isBigEndian()
{
unsigned short sh = 1;

char* ch = reinterpret_cast<char*>(&sh);

return (0 == *ch);
}

// ---------------
void showCompiler()
{
#if defined __hpux
system ("aCC -V");
#elif defined _AIX
system ("xlC -qversion");
#elif defined __sun
system ("CC -V");
#elif (defined __linux&& defined __INTEL_COMPILER)
system (" icpc -V");
#else
#error Undefined platform
#endif
}

// ---------------
void showEnv()
{
std::cout<< std::endl;
system ("uname -a");

std::cout<< std::endl;
showCompiler();
std::cout<< std::endl;

std::cout<< "sizeof(long) = "<< sizeof(long)<< std::endl;
std::cout<< std::endl;

std::cout<< "ENDIAN: "<< (isBigEndian() ? "BIG" : "LITTLE")<<
std::endl;
std::cout<< std::endl;
}


// -------------
void castTest()
{

#define SHOW(x) std::cout<< std::setw(2)<< std::left<< #x<<
std::right<< " = "<< std::hex<< std::showbase<< x<< std::dec<<
std::endl

unsigned long l = 0x13579bdf2458ace0;
unsigned short s1 = static_cast<unsigned short>(l);

unsigned short s2 = static_cast<const unsigned short&>(l);
unsigned short s3 = static_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));

unsigned short s4 = reinterpret_cast<const unsigned short&>(l);
unsigned short s5 = reinterpret_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));

SHOW(l);
SHOW(s1);
SHOW(s2);
SHOW(s3);
SHOW(s4);
SHOW(s5);

}


int main()
{
showEnv();
castTest();

return 0;
}


// ============ cast_test.cpp (END) ============



Compilation and running


// ====== cast test on HP-UX ======
// No errors

HP-UX hpx418 B.11.23 U ia64 1139467043 unlimited-user license

aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0xace0
s3 = 0xace0
s4 = 0x1357
s5 = 0x1357


// ====== cast test on SUN ======
// No errors

SunOS ilsun015 5.10 Generic_139555-08 sun4u sparc SUNW,SPARC-
Enterprise

CC: Sun C++ 5.10 SunOS_sparc 128228-08 2010/04/21
Usage: CC [ options ] files. Use 'CC -flags' for details

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0x1357
s3 = 0x1357
s4 = 0x1357
s5 = 0x1357


// ====== cast test on AIX ======
// No errors

AIX ep5512b 1 6 000497A2D900

IBM XL C/C++ for AIX, V10.1
Version: 10.01.0000.0000

sizeof(long) = 8

ENDIAN: BIG

l = 0x13579bdf2458ace0
s1 = 0xace0
s2 = 0xace0
s3 = 0xace0
s4 = 0x1357
s5 = 0x1357


// ====== cast test on Linix& INTEL-compiler ======

Linux illin025 2.6.18-92.1.22.el5 #1 SMP Fri Dec 5 09:28:22 EST 2008
x86_64 x86_64 x86_64 GNU/Linux

Intel(R) C++ Intel(R) 64 Compiler Professional for applications
running on Intel(R) 64, Version 11.0 Build 20090318 Package ID:
l_cproc_p_11.0.083
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.
cast_test.cpp(58): error: invalid type conversion: "unsigned long *"
to "const unsigned short&"
unsigned short s2 = static_cast<const unsigned short&>(l);
^

cast_test.cpp(59): error: invalid type conversion: "const unsigned
long *" to "const unsigned short&"
unsigned short s3 = static_cast<const unsigned
short&>(static_cast<const unsigned long&>(l));
^

compilation aborted for cast_test.cpp (code 2)

<comeau>
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 56: warning: integer conversion resulted in truncation
unsigned long l = 0x13579bdf2458ace0;
^

"ComeauTest.c", line 59: error: invalid type conversion
unsigned short s2 = static_cast<const unsigned short&>(l);
^

"ComeauTest.c", line 60: error: invalid type conversion
unsigned short s3 = static_cast<const unsigned
^

2 errors detected in the compilation of "ComeauTest.c".
</comeau>


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach /Usenet

J

James Kanze

SUN CC has a bug: prodices invalid result in
run-timehttp://forums.oracle.com/forums/thread.jspa?threadID=2191349&tstart
Intel icpc (v11) has a has: produces invalid error-message in
compile-timehttp://software.intel.com/en-us/forums/showpost.php?p=144859
Is there any problem with statement
static_cast<unsigned short&>(unsigned_long_type) ?

No, but using the results of the cast is undefined behavior.
 
J

James Kanze

What does Standard write of that?

That accessing an object of type unsigned long through an lvalue
expression is undefined behavior. Or more generally, that
accessing an object through an lvalue expression whose type is
neither that of the object nor a character type is undefined
behavior.
 
A

Alex Vinokur

That accessing an object of type unsigned long through an lvalue
expression is undefined behavior.  Or more generally, that
accessing an object through an lvalue expression whose type is
neither that of the object nor a character type is undefined
behavior.

Thanks.

What about reinterpret_cast<unsingned short&>(unsigned_long_type);
 
G

Gerhard Fiedler

Alex said:
Thanks.

What about reinterpret_cast<unsingned short&>(unsigned_long_type);

AIUI, the same: UB (which may be defined by your compiler).

reinterpret_cast doesn't make it more defined, it just gets rid of
compiler warnings :)

Gerhard
 

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
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top