Reputation: 2295
In c++11, a constexpr
expression cannot contain reinterpret casts. So for instance, if one wanted to manipulate the bits in a floating point number, say to find the mantissa of the number:
constexpr unsigned int mantissa(float x) {
return ((*(unsigned int*)&x << 9) >> 9);
};
The above code would fail to be constexpr
. In theory, I can't see how a reinterpret cast in this or similar cases can be any different from arithmetic operators, but the complier (and the standard) don't allow it.
Is there any clever way of getting around this limitation?
Upvotes: 49
Views: 23661
Reputation: 19140
Starting with C++20 there's a standard library solution: std::bit_cast
(supported in GCC since GCC 11). Here's an example of using it:
#include <bit>
#include <cstdint>
#include <iomanip>
#include <iostream>
int main()
{
constexpr float x=-235.23526f;
constexpr auto integer=std::bit_cast<std::uint32_t>(x);
constexpr auto mantissa=integer&0x7fffff;
static_assert(mantissa==0x6b3c3a);
std::cout << std::hex << std::setfill('0')
<< std::setw(8) << mantissa << "\n";
}
See its live demo.
Upvotes: 10
Reputation: 19140
Your particular example of getting a mantissa of a float
number is actually quite simple to implement for numbers without type punning at all, and thus implement it in a constexpr
fashion. The only problem would be when you want to hack on NaNs.
Since you already rely on float
being the binary32
from IEEE 754, we can assume the same, but in another way — to present results. See the following code:
#include <limits>
constexpr float abs(float x) { return x<0 ? -x : x; }
constexpr int exponent(float x)
{
return abs(x)>=2 ? exponent(x/2)+1 :
abs(x)<1 ? exponent(x*2)-1 : 0;
}
constexpr float scalbn(float value, int exponent)
{
return exponent==0 ? value : exponent>0 ? scalbn(value*2,exponent-1) :
scalbn(value/2,exponent+1);
}
constexpr unsigned mantissa(float x)
{
return abs(x)<std::numeric_limits<float>::infinity() ?
// remove hidden 1 and bias the exponent to get integer
scalbn(scalbn(abs(x),-exponent(x))-1,23) : 0;
}
#include <iostream>
#include <iomanip>
#include <cstring>
int main()
{
constexpr float x=-235.23526f;
std::cout << std::hex << std::setfill('0');
// Show non-constexpr result to compare with
unsigned val; std::memcpy(&val,&x,sizeof val);
std::cout << std::setw(8) << (val&0x7fffff) << "\n";
// Now the sought-for constexpr result
constexpr auto constexprMantissa=mantissa(x);
std::cout << std::setw(8) << constexprMantissa << "\n";
}
See its live demo.
Upvotes: 13
Reputation: 60999
I can't see how a reinterpret cast in this or similar cases can be any different from arithmetic operators
It isn't portable.
You are probably aware of the fact that your code causes undefined behavior, since you dereference a type punned pointer and thus break strict aliasing. Moreover, since C++14, operations that would invoke undefined behavior aren't even constant expressions anymore and should thus produce a compiler error.
What you are basically trying to do is alias the float
object with an integral glvalue. The first step is to obtain that glvalue; the second to perform an lvalue-to-rvalue conversion.
In C++14, the first step is impossible to accomplish in constant expressions. reinterpret_cast
is explicitly forbidden. And casts to and from void*
, like static_cast<char const*>(static_cast<void const*>(&x))
, don't work either (N3797, [expr.const]/2*):
— a conversion from type cv
void *
to a pointer-to-object type;
Keep in mind that a c-style cast like (char*)
is reduced to either static_cast
or reinterpret_cast
whose limitations are listed above. (unsigned*)&x
therefore reduces to reinterpret_cast<unsigned*>(&x)
and doesn't work.
In C++11, the cast to void const*
and then to char const*
does not constitute a problem (according to standard; Clang still complains about the latter). The lvalue-to-rvalue conversion is one nonetheless:
an lvalue-to-rvalue conversion (4.1) unless it is applied to
— a glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression, or
— a glvalue of literal type that refers to a non-volatile object defined withconstexpr
, or that refers to a sub-object of such an object, or
— a glvalue of literal type that refers to a non-volatile temporary object whose lifetime has not ended, initialized with a constant expression;
The first two bullets can't apply here; Neither has any char
/unsigned
/etc. object been initialized precedingly, nor did we define any such object with constexpr
.
The third bullet doesn't apply either. If we write
char ch = *(char const*)(void const*)&x;
we don't create a char
object in the initializer. We access the stored value of x
through a glvalue of type char
, and use that value to initialize ch
.
Therefore I'd say that such aliasing isn't possible in constant expressions. You may get around this in some implementations with relaxed rules.
* The paragraph is a list that starts with something like
A conditional-expression is a core constant expression unless [...]
(The text differs from N3337 to N3797.)
Upvotes: 22