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

Discussion in 'C++' started by Alex Vinokur, Mar 18, 2011.

  1. Alex Vinokur

    Alex Vinokur Guest

    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)
     
    Alex Vinokur, Mar 18, 2011
    #1
    1. Advertising

  2. 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>
     
    Alf P. Steinbach /Usenet, Mar 18, 2011
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Mar 19, 2011
    #3
  4. 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/thread.jspa?threadID=2191349&tstart
    >
    > Intel icpc (v11) has a has: produces invalid error-message in compile-time
    > http://software.intel.com/en-us/forums/showpost.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>
     
    Alf P. Steinbach /Usenet, Mar 19, 2011
    #4
  5. Alex Vinokur

    Walter H. Guest

    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/thread.jspa?threadID=2191349&tstart
    >
    > Intel icpc (v11) has a has: produces invalid error-message in
    > compile-time http://software.intel.com/en-us/forums/showpost.php?p=144859
    >
    > Is there any problem with statement
    > static_cast<unsigned short&>(unsigned_long_type) ?


    this statement is nonsens;
     
    Walter H., Mar 19, 2011
    #5
  6. Alex Vinokur

    James Kanze Guest

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

    On Mar 19, 6:20 am, "Alex Vinokur" <>
    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
     
    James Kanze, Mar 20, 2011
    #6
  7. Alex Vinokur

    Alex Vinokur Guest

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

    On Mar 20, 5:02 pm, James Kanze <> wrote:
    > On Mar 19, 6:20 am, "Alex Vinokur" <>
    > 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
     
    Alex Vinokur, Mar 21, 2011
    #7
  8. Alex Vinokur

    James Kanze Guest

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

    On Mar 21, 5:00 am, Alex Vinokur <> wrote:
    > On Mar 20, 5:02 pm, James Kanze <> wrote:


    > > On Mar 19, 6:20 am, "Alex Vinokur" <>
    > > 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
     
    James Kanze, Mar 22, 2011
    #8
  9. Alex Vinokur

    Alex Vinokur Guest

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

    On Mar 22, 2:17 am, James Kanze <> wrote:
    > On Mar 21, 5:00 am, Alex Vinokur <> wrote:
    >
    > > On Mar 20, 5:02 pm, James Kanze <> wrote:
    > > > On Mar 19, 6:20 am, "Alex Vinokur" <>
    > > > 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);
     
    Alex Vinokur, Mar 22, 2011
    #9
  10. 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 <> wrote:
    >> On Mar 21, 5:00 am, Alex Vinokur <> wrote:
    >>
    >>> On Mar 20, 5:02 pm, James Kanze <> wrote:
    >>> > On Mar 19, 6:20 am, "Alex Vinokur" <>
    >>> > 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
     
    Gerhard Fiedler, Mar 22, 2011
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    831
    Kaz Kylheku
    Oct 17, 2006
  2. Bo Peng
    Replies:
    11
    Views:
    1,094
    Victor Bazarov
    Oct 20, 2006
  3. Jim Langston
    Replies:
    10
    Views:
    791
    Frederick Gotham
    Nov 22, 2006
  4. Mark Kremers

    Behaviors in Behaviors

    Mark Kremers, Jul 31, 2003, in forum: Javascript
    Replies:
    0
    Views:
    119
    Mark Kremers
    Jul 31, 2003
  5. junyangzou
    Replies:
    13
    Views:
    259
Loading...

Share This Page