Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
7568 Discussions

Compilation error with unsigned short s = static_cast<const unsigned short&>(unsigned_long_type

alexvi
Beginner
201 Views

Hi,

Here is some code that was test on 4 compilers: icpc on Linux, aCC on HP-UX, CC on SUN, xlC on AIX

The code was compile without errors on HPUX aCC, SUN CC, AIX xlC, buf failed on icpc.

==========================================
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 C++ Intel 64 Compiler Professional for applications running on Intel 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(l);

^

cast_test.cpp(59): error: invalid type conversion: "const unsigned long *" to "const unsigned short &"

unsigned short s3 = static_cast(static_cast(l));

^

compilation aborted for cast_test.cpp (code 2)


// ============= cast_test.cpp =============

#include

#include

#include

bool isBigEndian()

{

unsigned short sh = 1;

char* ch = reinterpret_cast(&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(l);

unsigned short s2 = static_cast(l);

unsigned short s3 = static_cast(static_cast(l));

unsigned short s4 = reinterpret_cast(l);

unsigned short s5 = reinterpret_cast(static_cast(l));

SHOW(l);

SHOW(s1);

SHOW(s2);

SHOW(s3);

SHOW(s4);

SHOW(s5);

}

int main()

{

showEnv();

castTest();

return 0;

}

// =======================================

Regards,

Alex

0 Kudos
2 Replies
Judith_W_Intel
Employee
201 Views

I can reproduce these errorsin version 11.1 but not 12.0. Please upgrade to 12.0 if you would like a fix for this.

Judy
Om_S_Intel
Employee
201 Views
Reply