35

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?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

29

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.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 4
    I think you're right. `[C++03: 5.2.10/1]` clearly states that only the listed conversions are valid, and this self-conversion is not one of them. Hence, this is not a VS bug so much as a C++11 feature as-yet-unimplemented. – Lightness Races in Orbit Sep 18 '13 at 12:21
24

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105