C++
VisualStudio2013
VisualStudio2015

Visual Studioのreinterpret_castは盛大にバグっている

More than 1 year has passed since last update.

まあタイトルで大体話が終わっているんですが

int main() 
{ 
    int a = reinterpret_cast<int>(-1); 
}

こんなコードが通りません。

source.cpp(3): error C2440: 'reinterpret_cast' : cannot convert from 'int' to 'int'

Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast

こんなふうに言われます。

ところで本当にバグか不安なのでTwitterで聞いたところ

ん?§5.2.10 p2ってなんだ

§5.2.10 p2

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.

これか。

そんなわけでバグレポ投げました

https://connect.microsoft.com/VisualStudio/feedback/details/2692140/cannot-convert-integral-type-to-own-type-by-reinterpret-cast

追記

なんだって!?

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3086.html#UK55

ID Ref Comment Proposed Resolution Owner Issue Disposition
UK 55,[236] 5.2.10,¶,2 dynamic_cast and reinterpret_cast,crossreference 5.2.11 without creating an extra note. The,second half of the note is unrelated to the crossrefernce,,and would serve as well in normative text. Strike the note,about definition of casting away constness, preserve the,cross-reference. The second sentance on reintrepret_cast to,its own type should move out of the note into the normative,text. CWG 799 ACCEPTED,The repetition of the reference to 5.2.11 was handled as a quasi-editorialchange in paper N2841.,Issue 799 was opened for the question about castingto the same type as the operand.,See paper,N2841

たしかにイギリスからNational Body commentsとして出ていて、

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#799
799. Can reinterpret_cast be used to cast an operand to its own type?
Section: 5.2.10 [expr.reinterpret.cast] Status: CD2 Submitter: UK Date: 3 March, 2009

N2800 comment UK 55

[Voted into WP at March, 2010 meeting.]

The note in 5.2.10 [expr.reinterpret.cast] paragraph 2 says,

Subject to the restrictions in this section, an expression may be cast to its own type using a reinterpret_cast operator.

However, there is nothing in the normative text that permits this conversion, and paragraph 1 forbids any conversion not explicitly permitted.

(See also issue 944.)

Proposed resolution (October, 2009):

  1. Change 5.2.10 [expr.reinterpret.cast] paragraph 2 as follows:

    The reinterpret_cast operator shall not cast away constness (5.2.11 [expr.const.cast]). [Note: Subject to the restrictions in this section, an expression may be cast to its own type using a reinterpret_cast operator. —end note] An expression of integral, enumeration, pointer, or pointer-to-member type can be explictly converted to its own type; such a cast yields the value of its operand.

  2. Change 5.2.10 [expr.reinterpret.cast] paragraph 10 as follows:

    An rvalue of type “pointer to member of X of type T1” can be explicitly converted to an rvalue of a different type “pointer to member of Y of type T2” if T1 and T2 are both function types or both object types...

Core IssueとなりめでたくC++11で修正が入っているようだ。