【发布时间】:2018-01-19 12:29:52
【问题描述】:
我们有:
template <typename ...T> concept bool Numerics = ( std::is_arithmetic_v<T> && ... ) ;
template <typename T> concept bool Numeric = std::is_arithmetic_v<T>;
所以我们可以使用如下的 requires 子句来应用类型约束:
template <typename T, typename U, typename V, typename W> requires Numerics<T,U,V,W>
auto foo(T arg1, U arg2, V arg3, W arg4) {
return arg1 + arg2 + arg3 + arg4;
}
但是我们不能这样写模板介绍格式:
// err: no match concept
//
// Numerics{T,U,V,W}
// auto foo2(T arg1, U arg2, V arg3, W arg4) {
// return arg1 + arg2 + arg3 + arg4;
// }
必须明确定义固定数量的参数:
template <typename T, typename U, typename V, typename W>
concept bool Numeric4 = Numerics<T,U,V,W>;
Numeric4{T,U,V,W}
auto foo3(T arg1, U arg2, V arg3, W arg4) {
return arg1 + arg2 + arg3 + arg4;
}
为什么template <typename ...T> concept 不能在模板介绍格式中工作,而在 requires 子句中工作?
【问题讨论】:
标签: c++ templates variadic-templates c++-concepts