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