问题描述
在探索此答案时,我发现带有参数包的模板不会被期望的模板接受具有特定数量参数的模板.
While exploring this answer I discovered that a template that takes a parameter pack will not be accepted by a template that expects template that has a specific number of parameters.
在我看来,这是一个缺陷,因为如果模板可以接受任意数量的参数,则它应该能够映射到特定数量.是否有语言律师可以解释为什么不允许这样做?
This seems to me that it is a defect since if a template can take any number of parameters, it should be able to map to a specific number. Is there a language lawyer that could explain why this is not allowed?
这是一个简单的例子:
template <typename...Ts>
using pack = void;
template <template <typename> class>
using accept_template = int;
accept_template<pack> value = 0;
我当然不会在这种确切的情况下使用它.它将用于将模板传递给另一个模板,该模板将以某种方式使用传递的模板.在我链接的答案中,我已经提出了一种解决方法,但是我仍然觉得这是一个缺陷.
I wouldn't use it in this exact scenario of course. It would be used to pass a template to another template which would use the passed template in some manner. In my answer that I linked, I have stated a workaround, but I still feel that this is a defect.
推荐答案
由于 P0522 ,其中引入了一些新规则来处理模板模板参数如何匹配模板模板参数.结果,从论文中得出:
This restriction was loosened as a result of P0522, which introduces new rules to handle how template template-arguments match template template-parameters. As a result, from the paper:
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中编译.
Your example fails to compile in C++14, but will compile in C++17.
这篇关于与模板< typename ...>不匹配模板< typename>有缺陷吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!