まあタイトルで大体話が終わっているんですが
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で聞いたところ
@yumetodo §5.2.10 p2により整数型を自分自身の型にキャストすることは合法。
— Ryou Ezoe (@EzoeRyou) 2016年5月11日
ん?§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.
これか。
そんなわけでバグレポ投げました
追記
@EzoeRyou @yumetodo C++03 の規定に従えばエラーにする必要があったところで、それはおかしいということで C++11 で同じ型への変換を許すように改良されてました。https://t.co/fxHwMopTmO
— Kazutoshi SATODA (@k_satoda) 2016年5月12日
MSVC がこれに対応できていないようです。
なんだって!?
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
[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_castoperator.
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):
- Change 5.2.10 [expr.reinterpret.cast] paragraph 2 as follows:
The
reinterpret_castoperator 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 aAn 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.reinterpret_castoperator. —end note]
- Change 5.2.10 [expr.reinterpret.cast] paragraph 10 as follows:
An rvalue of type “pointer to member of
Xof typeT1” can be explicitly converted to an rvalue of a different type “pointer to member ofYof typeT2” ifT1andT2are both function types or both object types...
Core IssueとなりめでたくC++11で修正が入っているようだ。