Via*_*rus 6 c++ gcc constexpr c++14
GCC无法将某些表达式评估为常量.然而,Clang很不错.
/*
*/
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");
Run Code Online (Sandbox Code Playgroud)
也行不通:
constexpr int foobar()
{
int array[100] = {};
int *ar = array;
if (ar == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foobar() == 1, "okay");
Run Code Online (Sandbox Code Playgroud)
一样:
constexpr int foo2()
{
int *a = nullptr;
if (a == nullptr) // Error...
{
return 0;
}
return 1;
}
static_assert(foo2() == 0, "okay");
Run Code Online (Sandbox Code Playgroud)
我的意思是,比较nullptr应该是别的,而不是与其他随机地址的比较.
你会说:这是一个错误还是一个解释问题?对我来说,很难为两个编译器编写相同的代码......
GCC 5.0到5.4发生此错误.在GCC 6+中只foobar()编译.