在探索的同时这个答案 https://stackoverflow.com/a/47730794/1366368我发现采用参数包的模板不会被需要具有特定数量参数的模板的模板所接受。
在我看来,这是一个缺陷,因为如果模板可以采用任意数量的参数,它应该能够映射到特定的数量。有语言律师可以解释为什么这是不允许的吗?
这是一个简单的例子:
template <typename...Ts>
using pack = void;
template <template <typename> class>
using accept_template = int;
accept_template<pack> value = 0;
当然,我不会在这种情况下使用它。它将用于将模板传递给另一个模板,该模板将以某种方式使用传递的模板。在我链接的答案中,我已经提出了解决方法,但我仍然认为这是一个缺陷。
这一限制被放宽是因为P0522 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0522r0.html,它引入了新的规则来处理模板模板参数如何匹配模板模板参数。结果,从论文中可以看出:
template<class T, class U = T> class B { /* ... */ };
template <class ... Types> class C { /* ... */ };
template<template<class> class P> class X { /* ... */ };
X<B> xb; // OK, was ill-formed:
// default arguments for the parameters of a template argument are ignored
X<C> xc; // OK, was ill-formed:
// a template parameter pack does not match a template parameter
您的示例无法在 C++14 中编译,但会在 C++17 中编译。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)