假设我有这些模板别名:
enum class enabler {};
template <typename T>
using EnableIf = typename std::enable_if<T::value, enabler>::type;
template <typename T>
using DisableIf = typename std::enable_if<!T::value, enabler>::type;
我可以在 GCC 中执行以下操作:
#include <iostream>
template <typename T, EnableIf<std::is_polymorphic<T>> = {}>
void f(T) { std::cout << "is polymorphic\n"; }
template <typename T, DisableIf<std::is_polymorphic<T>> = {}>
void f(T) { std::cout << "is not polymorphic\n"; }
struct foo { virtual void g() {} };
int main() {
f(foo {});
f(int {});
}
它打印:
是多态的
不是多态的
这符合我的期望。
使用 clang 该代码无法编译。它会产生以下错误消息。
test.cpp:11:58: error: expected expression
template <typename T, EnableIf<std::is_polymorphic<T>> = {}>
^
test.cpp:14:59: error: expected expression
template <typename T, DisableIf<std::is_polymorphic<T>> = {}>
^
test.cpp:20:3: error: no matching function for call to 'f'
f(foo {});
^
test.cpp:12:6: note: candidate template ignored: couldn't infer template argument ''
void f(T) { std::cout << "is polymorphic\n"; }
^
test.cpp:15:6: note: candidate template ignored: couldn't infer template argument ''
void f(T) { std::cout << "is not polymorphic\n"; }
^
test.cpp:21:3: error: no matching function for call to 'f'
f(int {});
^
test.cpp:12:6: note: candidate template ignored: couldn't infer template argument ''
void f(T) { std::cout << "is polymorphic\n"; }
^
test.cpp:15:6: note: candidate template ignored: couldn't infer template argument ''
void f(T) { std::cout << "is not polymorphic\n"; }
^
4 errors generated.
应该编译吗?这两个编译器哪个有问题?
First and foremost, thanks to @Richard Smith https://stackoverflow.com/users/1041090/richard-smith on the #llvm IRC Channel on oftc http://llvm.org/docs/#irc for the explanation.
Unfortunately, this is not legal C++ and as such Clang is correct: {}
is not an expression but a braced-init-list and as such will never be a constant expression as is needed in the initializer of a non-type template parameter.
§14.3.2 [temp.arg.non-type] p1
A 模板参数对于非类型、非模板模板参数应为以下之一:
- 对于非类型模板参数整型或枚举类型,转换后的常量表达式(5.19) 的类型模板参数; or
- [...]
一种解决方案是设置一个虚拟值enabler
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)