是否允许多个非类型模板参数包? [英] Are multiple non-type template parameter packs allowed?

查看:94
本文介绍了是否允许多个非类型模板参数包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[temp.param] p11说(在N4527中):

[temp.param] p11 says (in N4527):

(...)除非可以从功能模板的parameter-type-list推导该模板参数或具有默认参数,否则该功能模板的模板参数包后不得再有另一个模板参数.

(...) A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list of the function template or has a default argument

非类型模板参数包的上下文中,没有默认参数,
那么究竟需要为包装推导出什么(也就是类型或值)呢?

In the context of non-type template parameter packs, there can't be default arguments,
so what exactly needs to be deduced for the packs (just the type, or the values, too)?

即我想知道标准(C ++ 11、14或1z)是否允许这样做:

i.e. I'm wondering if this is allowed by the standard (C++11, 14 or 1z):

template<typename T, T... A, T... B>
void foo(T) {}

可以明确指定第一个包的值,但是第二个包是无法访问的",如果我没记错的话,它总是空的.

The first pack's values could be explicitly specified, but the second pack is "unreachable" and would always be empty if I'm not mistaken.

clang ++-3.6和g ++-5.2似乎接受了这些空的无法到达的包(甚至是非非类型的包),但是VC ++ 14.0拒绝了它们,并报错:

clang++-3.6 and g++-5.2 seem to accept these empty unreachable packs (even non non-type packs), but VC++ 14.0 refuses them with the error:

错误C3547:无法使用模板参数'B',因为它遵循模板参数包,并且不能从'foo'的功能参数中推导出来

error C3547: template parameter 'B' cannot be used because it follows a template parameter pack and cannot be deduced from the function parameters of 'foo'

推荐答案

否,该标准不允许.来自[temp.param]:

No, it not allowed by the standard. From [temp.param]:

不得遵循功能模板的模板参数包 除非可以从 parameter-type-list 推导出该模板参数,否则使用另一个模板参数 函数模板的名称或具有默认参数(14.8.2). [示例:

A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list of the function template or has a default argument (14.8.2). [Example:

template<class T1 = int, class T2> class B;    // error

// U can be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { }  // error
template<class... T, class U> void g() { }     // error

-结束示例]

在您的情况下,无法推论...B(因为没有任何推论依据),并且没有默认参数.

In your case, ...B cannot be deduced (as there's nothing to deduce it from) and it has no default argument.

那么对于包装到底需要推算什么(也就是类型或值)?

so what exactly needs to be deduced for the packs (just the type, or the values, too)?

例如,如果您的foo是:

template <size_t... A, size_t... B>
void foo(std::index_sequence<B...> );

...A后跟...B,但是可以推导出...B,因此可以.同样:

...A is followed by ...B, but ...B can be deduced, so that's allowed. Similarly:

template <typename T, T... A, T B = 0>
void foo();

很好,因为B具有默认参数.

is fine, as B has a default argument.

这篇关于是否允许多个非类型模板参数包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆