The following part of the code was successfully compiled with gcc 5.3.0, but not compiled with clang 3.7.0. I used coliru online compilers with the same command line options in both cases: -std = C ++ 14 -O2 -Wall -pedantic -pthread.
#include <cstdio> // Definition of constexpr function 'foo'. constexpr std::size_t foo(const int& arg_foo) { return sizeof(arg_foo); } // Definition of function 'test'. void test(const int& arg) { // The following line produces an error with clang. constexpr std::size_t res_foo = foo(arg); // Print the result returned by the 'foo' function. std::printf("res_foo = %lu\n", res_foo); } // Definition of function 'main'. int main(int argc, const char* argv[]) { // Test function call. test(argc); // Return statement. return 0; }
clang rejects it with the following error:
error: constexpr variable 'res_foo' must be initialized by a constant expression constexpr size_t res_foo = foo(arg); ~~~~^~~~
Due to this difference between the two compilers, I wonder if this is really a valid piece of code. If not, I would like to better understand why this is so.
source share