Question

My compiler is the latest VC++ 2013 RC.

void f()
{
    int n1 = 0;
    int n2 = reinterpret_cast<int>(n1); // error C2440
}

error C2440: 'reinterpret_cast' : cannot convert from 'int' to 'int'

Why can reinterpret_cast not be used in such an obvious case?

Was it helpful?

Solution

According to cppreference.com the following conversion is available only since C++11:

An expression of integral, enumeration, pointer, or pointer-to-member type can be converted to its own type. The resulting value is the same as the value of expression.

which may not be implemented in Visual Studio 2013 RC yet.

OTHER TIPS

The C++ standard says (5.2.10.2) (emphasis mine):

The reinterpret_cast operator shall not cast away constness (5.2.11). An expression of integral, enumeration, pointer, or pointer-to-member type can be explicitly converted to its own type; such a cast yields the value of its operand.

So I'd say it's a bug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top