简体   繁体   English

为什么可变参数模板在模板引入中不起作用,而在需求子句中起作用? ConceptName {T,U,V,W} &lt;—模板 <typename …T>

[英]Why does variadic template not work in template introduction but work in requires-clause? ConceptName{T,U,V,W} <— template<typename …T>

We have: 我们有:

template <typename ...T> concept bool Numerics = ( std::is_arithmetic_v<T> && ... ) ;
template <typename T>    concept bool Numeric  =   std::is_arithmetic_v<T>;

So we can apply type constraints with a requires-clause like this: 因此,我们可以将类型约束与如下所示的require子句一起应用:

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;
}

But we can not write in template introduction format like this: 但是我们不能以这样的模板介绍格式编写:

// err: no match concept
// 
// Numerics{T,U,V,W}
// auto foo2(T arg1, U arg2, V arg3, W arg4) {
//     return arg1 + arg2 + arg3 + arg4;
// }

Having to explicitly define fixed number of arguments instead: 必须显式定义固定数量的参数:

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;
}

Why doesn't template <typename ...T> concept work in template introduction format while work in requires-clause? 为什么template <typename ...T> concept按模板介绍格式工作,而按需子句工作呢?

LIVE 生活

First, note that this syntax has been removed from the Concepts TS in its latest draft . 首先,请注意,此语法已从Concepts TS的最新草案中删除。


In the previous draft , this syntax was defined in [temp.intro] and is well-defined: 在上一个草案中 ,此语法在[temp.intro]中定义,并且定义明确:

Numerics{T,U,V,W}
auto foo2(T arg1, U arg2, V arg3, W arg4) {
    return arg1 + arg2 + arg3 + arg4;
}

should, for each introduced-parameter , adjust the parameter pack in Numerics by its pattern, and declare a template parameter based on that pattern. 应该对每个引入的参数 ,调整参数组Numerics由它的模式,并宣布基于该模式的模板参数。 So this should be equivalent to: 因此,这应等效于:

template <typename T, typename U, typename V, typename W> // per [temp.intro]/2
     requires Numerics<T,U,V,W> // per [temp.intro]/5
auto foo2(T arg1, U arg2, V arg3, W arg4) {
    return arg1 + arg2 + arg3 + arg4;
}

There are further examples in this section which illustrate that this should work. 本节中还有其他示例说明了这应该起作用。 Per this draft, the code is well-formed. 根据该草案,代码格式正确。


That said, as previously noted, the syntax has been removed from the TS and does not appear in the C++20 working draft. 就是说,如前所述,该语法已从TS中删除,并且未出现在C ++ 20工作草案中。 It may or may not be added in the future, in this form or otherwise. 将来可能会或可能不会以这种形式或其他方式添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM