次のコードは、gcc 5.3.0で正常にコンパイルされましたが、clang 3.7.0でコンパイルできませんでした。私は両方の場合に同じコマンドラインオプションを使ってオンラインcoliruコンパイラを使用しました:-std = C++ 14 -O2 -Wall -pedantic -pthread。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;
}
打ち鳴らすには、次のエラーでそれを拒否:これは、コードの有効な部分である場合、私は疑問に思って、そのため2つのコンパイラとのこの違いの
error: constexpr variable 'res_foo' must be initialized by a constant expression
constexpr size_t res_foo = foo(arg);
~~~~^~~~
。そうでない場合は、なぜこれが当てはまるのかをよりよく理解したいと思います。
gcc bug、constexprについては多くの人のうちの1人 – TemplateRex
@TemplateRexしかし、 'foo'はどのようにしてconstexpr関数の規則を破りますか? – Archimaredes
@Archimaredesいいえ、 'test'の' arg'は 'constexpr'ではありません – TemplateRex