【发布时间】:2016-04-06 08:06:56
【问题描述】:
以下程序可以使用 GCC 5.2 编译,但不能使用 clang 3.6:
constexpr bool flag();
template <bool b = flag()>
constexpr bool test()
{
return b;
}
int main()
{
}
我用 clang 得到的错误信息是:
main.cpp:3:20: error: non-type template argument is not a constant expression
template <bool b = flag()>
^~~~~~
main.cpp:3:20: note: undefined function 'flag' cannot be used in a constant expression
main.cpp:1:16: note: declared here
constexpr bool flag();
^
main.cpp:4:16: error: no return statement in constexpr function
constexpr bool test()
^
我的问题是:谁是对的?或者,换句话说:程序是否格式错误?
【问题讨论】:
-
可以确认,连clang 3.7都不编译。但是,如果您将 return 语句放入
constexpr bool flag(),它会起作用。 -
我认为这与您关于评估默认模板参数的点的其他问题有关:如果在定义时对它们进行评估,则 clang 无法证明此模板在 [ temp.res]p8
-
为什么在void函数中有return语句?我还可以确认这在 MSVS 2015 上编译
-
@0x499602D2 对不起,我有点困惑。这个模板当然可以有有效的专业化。所以问题是,您是否可以提供无效的默认模板参数。 CWG 2008 和 CWG 1850 触及了这一点,但我认为它没有得到完全回答。 member templates 中也有类似的问题,表示允许clang 拒绝OP 的程序。
-
我很高兴我不以编写 C++ 编译器为生。
标签: c++ templates c++11 language-lawyer constexpr