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

  2. * Alex Vinokur, on 18.03.2011 06:11:
    <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
     
    Alf P. Steinbach /Usenet, Mar 18, 2011
    #2
    1. Advertisements

  3. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Mar 19, 2011
    #3
  4. Alf P. Steinbach /Usenet, Mar 19, 2011
    #4
  5. Alex Vinokur

    Walter H. Guest

    Walter H., Mar 19, 2011
    #5
  6. Alex Vinokur

    James Kanze Guest

    No, but using the results of the cast is undefined behavior.
     
    James Kanze, Mar 20, 2011
    #6
  7. Alex Vinokur

    Alex Vinokur Guest

    What does Standard write of that?

    Alex
     
    Alex Vinokur, Mar 21, 2011
    #7
  8. Alex Vinokur

    James Kanze Guest

    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, Mar 22, 2011
    #8
  9. Alex Vinokur

    Alex Vinokur Guest

    Thanks.

    What about reinterpret_cast<unsingned short&>(unsigned_long_type);
     
    Alex Vinokur, Mar 22, 2011
    #9
  10. 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. Advertisements

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 (here). After that, you can post your question and our members will help you out.