Poniższy fragment kodu został pomyślnie skompilowany z gcc 5.3.0, ale nie udało się skompilować z clang 3.7.0. Użyłem kompilatorów online coliru z tymi samymi opcjami wiersza poleceń w obu przypadkach: -std = C++ 14 -O2 -Wall -pedantic -pthread.Argument używany tylko w nieocenionym kontekście w ciele funkcji constexpr
#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;
}
dzyń odrzuca je z powodu następującego błędu:
error: constexpr variable 'res_foo' must be initialized by a constant expression
constexpr size_t res_foo = foo(arg);
~~~~^~~~
Z powodu tej różnicy pomiędzy tymi dwoma kompilatorów, zastanawiam się, czy jest to ważny kawałek kodu. Jeśli nie, chciałbym lepiej zrozumieć, dlaczego tak się dzieje.
gcc bug, jeden z wielu, jeśli chodzi o constexpr – TemplateRex
@TemplateRex Ale w jaki sposób 'foo' łamie którąkolwiek z zasad funkcji constexpr? – Archimaredes
@Archimaredes nie, błąd polega na tym, że 'arg' w' teście' nie jest 'constexpr' – TemplateRex