[temp.param] p11 说(在 N4527 中):
(...) 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
在非类型模板参数包的上下文中,不能有默认参数,
那么究竟需要为包推导出什么(只是类型,还是值)?
即我想知道标准(C++11、14 或 1z)是否允许这样做:
template<typename T, T... A, T... B>
void foo(T) {}
第一个包的值可以明确指定,但第二个包是“无法访问的”,如果我没记错的话,它总是空的。
clang++-3.6 和 g++-5.2 似乎接受这些空的无法访问的包(甚至是非非类型包),但 VC++ 14.0 拒绝它们并出现错误:
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]:
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
—end example ]
在你的情况下, ...B
不能被推导出来(因为没有任何东西可以推导出来)并且它没有默认参数。
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
,所以这是允许的。同样:
template <typename T, T... A, T B = 0>
void foo();
很好,因为 B
有一个默认参数。
关于c++ - 是否允许多个非类型模板参数包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31697055/