Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   static_cast<unsigned short&>(long_type): 4 different compilers - 3different behaviors for that statement (http://www.velocityreviews.com/forums/t745325-static_cast-unsigned-short-and-long_type-4-different-compilers-3different-behaviors-for-that-statement.html)

Alex Vinokur 03-18-2011 05:11 AM

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


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


> ./a.out


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


> ./a.out


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)


Alf P. Steinbach /Usenet 03-18-2011 06:00 AM

Re: static_cast<unsigned short&>(long_type): 4 different compilers- 3 different behaviors for that statement
 
* 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 ======
>
>> > aCC +DD64 -AA cast_test.cpp

> // No errors
>
>
>> > ./a.out

> 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
>
>
>> > ./a.out

> 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)
>


<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

--
blog at <url: http://alfps.wordpress.com>

Alex Vinokur 03-19-2011 06:20 AM

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

SUN CC has a bug: prodices invalid result in run-time
http://forums.oracle.com/forums/thre...2191349&tstart

Intel icpc (v11) has a has: produces invalid error-message in compile-time
http://software.intel.com/en-us/foru...t.php?p=144859

Is there any problem with statement
static_cast<unsigned short&>(unsigned_long_type) ?

Alex




Alf P. Steinbach /Usenet 03-19-2011 02:21 PM

Re: static_cast<unsigned short&>(long_type): 4 different compilers- 3 different behaviors for that statement
 
* Alex Vinokur, on 19.03.2011 07:20:
>
> SUN CC has a bug: prodices invalid result in run-time
> http://forums.oracle.com/forums/thre...2191349&tstart
>
> Intel icpc (v11) has a has: produces invalid error-message in compile-time
> http://software.intel.com/en-us/foru...t.php?p=144859
>
> Is there any problem with statement
> static_cast<unsigned short&>(unsigned_long_type) ?


Are you deaf or something?

I posted reply to you earlier.

Read it.


- Alf

--
blog at <url: http://alfps.wordpress.com>

Walter H. 03-19-2011 05:14 PM

Re: static_cast<unsigned short&>(long_type): 4 different compilers- 3 different behaviors for that statement
 
Alex Vinokur wrote:

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


this statement is nonsens;


James Kanze 03-20-2011 03:02 PM

Re: static_cast<unsigned short&>(long_type): 4 different compilers -3 different behaviors for that statement
 
On Mar 19, 6:20 am, "Alex Vinokur" <ale...@users.sourceforge.net>
wrote:
> 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.

--
James Kanze

Alex Vinokur 03-21-2011 05:00 AM

Re: static_cast<unsigned short&>(long_type): 4 different compilers -3 different behaviors for that statement
 
On Mar 20, 5:02*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Mar 19, 6:20 am, "Alex Vinokur" <ale...@users.sourceforge.net>
> wrote:
>
> > 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.
>
> --
> James Kanze


What does Standard write of that?

Alex



James Kanze 03-22-2011 12:17 AM

Re: static_cast<unsigned short&>(long_type): 4 different compilers -3 different behaviors for that statement
 
On Mar 21, 5:00 am, Alex Vinokur <alex.vino...@gmail.com> wrote:
> On Mar 20, 5:02 pm, James Kanze <james.ka...@gmail.com> wrote:


> > On Mar 19, 6:20 am, "Alex Vinokur" <ale...@users.sourceforge.net>
> > wrote:


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


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

--
James Kanze

Alex Vinokur 03-22-2011 04:44 AM

Re: static_cast<unsigned short&>(long_type): 4 different compilers -3 different behaviors for that statement
 
On Mar 22, 2:17*am, James Kanze <james.ka...@gmail.com> wrote:
> On Mar 21, 5:00 am, Alex Vinokur <alex.vino...@gmail.com> wrote:
>
> > On Mar 20, 5:02 pm, James Kanze <james.ka...@gmail.com> wrote:
> > > On Mar 19, 6:20 am, "Alex Vinokur" <ale...@users.sourceforge.net>
> > > wrote:
> > > > 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.

> > 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.
>
> --
> James Kanze


Thanks.

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




Gerhard Fiedler 03-22-2011 01:53 PM

Re: static_cast<unsigned short&>(long_type): 4 different compilers - 3 different behaviors for that statement
 
Alex Vinokur wrote:

> On Mar 22, 2:17*am, James Kanze <james.ka...@gmail.com> wrote:
>> On Mar 21, 5:00 am, Alex Vinokur <alex.vino...@gmail.com> wrote:
>>
>>> On Mar 20, 5:02 pm, James Kanze <james.ka...@gmail.com> wrote:
>>> > On Mar 19, 6:20 am, "Alex Vinokur" <ale...@users.sourceforge.net>
>>> > wrote:
>>> > > 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.
>>> 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.

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


All times are GMT. The time now is 05:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.