문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top