並ゲームプログラマの書き捨て場

ゲームプログラマやってる私の日記、雑記、備忘録、公開場所

相変わらず彼は規格に詳しくない

こういう決まりですので。djannです。


相変わらずの規格上の疑問である。ポインタ型からboolへの変換は、規格上認められた動作で、暗黙的に行えるという認識だったのだが、これは正しくはないのだろうか?

Visual Studio 2015で以下のコードをコンパイルすると警告が出る。

class foo {
	int *m_ptr;
	explicit operator bool() { return m_ptr; }
};
'int *': forcing value to bool 'true' or 'false'

int*をtrueまたはfalseのbool型に強制する。それのどこが問題なのだろうか?nullptrであればfalseに、そうでなければtrueに変換されることを明確に期待しているのだが、規格上正しくないのだろうか?


こちらが該当しそうな気がしていたのだが

4.12 Boolean conversions
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.