Constexpr: Comparison with nullptr - error or function?

GCC cannot evaluate some expression as constant. Klang, however, is pleased with him.

/* */ 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"); 

Also does not work:

 constexpr int foobar() { int array[100] = {}; int *ar = array; if (ar == nullptr) // Error... { return 0; } return 1; } static_assert(foobar() == 1, "okay"); 

Same:

 constexpr int foo2() { int *a = nullptr; if (a == nullptr) // Error... { return 0; } return 1; } static_assert(foo2() == 0, "okay"); 

Living example

I mean, a comparison with nullptr should be something other than a comparison with another random address.

Could you say: is this a mistake or a question of interpretation? It's hard for me to write the same code for both compilers ...

This error occurs for GCC 5.0 to 5.4. In GCC 6+, only foobar() does not compile.

+5
source share
1 answer

This is already fixed for gcc-7. I opened: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77539 to request a backup.

+3
source

All Articles