Re:C++14: floats, bits, and constant expressions
by satbyy on Monday March 28, 2016 @04:11AM
(#11373216)
I think the first example is sorta invalid because there's a typo:
should read as:
With this change, the code indeed compiles fine (with g++ 5.3.1; not sure of 5.3.0, which the author used)
[ Parent
| on HN ]
Re:C++14: floats, bits, and constant expressions
by Kristine1975 on Monday March 28, 2016 @05:14AM
(#11373335)
With clang 3.7.0 this results in "constexpr function never produces a constant expression", since the C-style pointer cast is essentially a reinterpret_cast (C++14 Standard §5.4p4.4), which is not a "core constant expression" (C++14 Standard §5.20p2.13) and thus must not appear in a constexpr function: C++14 Standard §7.1.5p5:
... if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, the program is ill-formed; no diagnostic required.
clang seems to go the extra mile and produces a diagnostic.
Incidentally the pointer cast would violate C++'s aliasing rules and thus invoke undefined behavior.
P.S: The C++14 Standard is available here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n429... (never mind the "draft" status)
Re:C++14: floats, bits, and constant expressions
by satbyy on Monday March 28, 2016 @06:06AM
(#11373453)
Interestingly, g++ gives out a warning (correctly) when compiled with -O2, but not by default (-O0):
warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
So, you're right indeed.
Re:C++14: floats, bits, and constant expressions
by mike_hock on Monday March 28, 2016 @08:59AM
(#11373958)
This is illegal (undefined), but casting to char* and reading out the values is legal.
Re:C++14: floats, bits, and constant expressions
by sebastic on Monday March 28, 2016 @05:47AM
(#11373413)
Please stop using C-style casts. It's 2016. C++ != C. Unlearn what you have learned.
Re:C++14: floats, bits, and constant expressions
by TickleSteve on Monday March 28, 2016 @06:22AM
(#11373490)
'idiomatic C++' != C