FreeBSD の LLVM で C 言語とC++ の小数型リテラル値の論理和を試す ― 2018年10月03日 12時06分32秒
そこで、まずは FreeBSD 11.2-RELEASE 上の LLVM で実験。
LLVM は double 型を _Bool 型に変換すると警告。_Bool 型ときて気になったのが C++。興味深いのは整数型リテラル値の論理和を試した時は、「warning: use of logical '&&' with constant operand]と警告されたのに、浮動小数点型の定数を渡すと、[-Wconstant-logical-operand] は警告されない。
freebsd% cat logical-and-double.c
int main()
{
return 1 && .2;
}
freebsd% cc logical-and-double.c
logical-and-double.c:3:17: warning: implicit conversion from 'double' to '_Bool'
changes value from 0.2 to true [-Wliteral-conversion]
return 1 && .2;
~~ ^~
1 warning generated.
C++ では bool 型に変換されるのだろうか。
との事で、bool 型に変換された。
freebsd% cat logical-and-double.cpp
int main()
{
return 1 && .2;
}
freebsd % c++ logical-and-double.cpp
logical-and-double.cpp:3:17: warning: implicit conversion from 'double' to
'bool' changes value from 0.2 to true [-Wliteral-conversion]
return 1 && .2;
~~ ^~
1 warning generated.
おまけで、c ファイルを c++ でコンパイルした時の結果。
c ファイルを c++ としてコンパイルしていると警告を出している。.2 が bool 型に変換されているのも証拠。
freebsd% c++ logical-and-double.c
c++: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
logical-and-double.c:3:17: warning: implicit conversion from 'double' to 'bool'
changes value from 0.2 to true [-Wliteral-conversion]
return 1 && .2;
~~ ^~
1 warning generated.
最近のコメント