GCC nie może ocenić niektórych wyrażeń jako stałych. Clang jednak jest z nim miły.Constexpr: Comparision to nullptr - Błąd lub funkcja?
/*
*/
constexpr int foo(const int * array)
{
if (array == nullptr) // Error: '(((const int*)(& array)) == 0u)' is not a constant expression
{
return 0;
}
return 1;
}
constexpr int bar()
{
int array[100] = {};
return foo(array);
}
static_assert(bar() == 1, "outch..."); // Does not compile. See above.
static_assert(foo(nullptr) == 0, "okay");
constexpr int i[100] = {};
static_assert(foo(i) == 1, "okay");
Czy też nie działa:
constexpr int foobar()
{
int array[100] = {};
int *ar = array;
if (ar == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foobar() == 1, "okay");
samo:
constexpr int foo2()
{
int *a = nullptr;
if (a == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foo2() == 0, "okay");
Znaczy, porównanie do nullptr
powinno być coś innego niż porównanie do inny losowy adres.
Czy powiedziałbyś: to błąd lub kwestia interpretacji? Dla mnie trudno jest napisać ten sam kod dla obu kompilatorów ...
Ten błąd występuje w GCC 5.0 do 5.4. W GCC 6+ tylko foobar()
nie kompiluje się.