GCC, bazı ifadeleri sabit olarak değerlendiremiyor. Clang bununla birlikte güzel.Constexpr: nullptr ile karşılaştırma - Hata mı yoksa özellik mi?
/*
*/
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");
da çalışmaz:
constexpr int foobar()
{
int array[100] = {};
int *ar = array;
if (ar == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foobar() == 1, "okay");
Aynı şey: Yani
constexpr int foo2()
{
int *a = nullptr;
if (a == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foo2() == 0, "okay");
, nullptr
bir karşılaştırma bir bir karşılaştırma daha başka bir şey olmalı diğer rastgele adres.
Şunu diyecek misiniz: bir hata mı, yoksa bir yorum mu? Benim için her iki derleyici için de aynı kodu yazmak zordur ...
Bu hata GCC 5.0 ila 5.4 için geçerlidir. GCC 6 + 'da sadece foobar()
derleme yapmıyor.