I'm using __builtin_expect to perform a null pointer check in a constexpr function like so:
constexpr int Add(const int * x, const int * y)
{
assert(__builtin_expect(!!(x && y), 1));
return *x + *y;
}
I also have a wrapper method for const refs to allow for passing temporaries:
constexpr int Add(const int & x, const int & y)
{
return Add(&x, &y);
}
I then create a global constant using temporaries:
static constexpr int result = Add(1, 2);
Printing this value results in 0:
int main() {
std::cout << result; // 0
}
What exactly is going on here?
Removal of __builtin_expect fails to compile with GCC complaining about pointer comparisons to temporaries inside constexpr context maybe related.
__builtin_expect(x,y)is a compiler extension, but I think it is supposed to work like this. If you replace it withx == y, then I don't see any issue with the program. It should compile and produce3. So I'll say it is a bug in the compiler.