不匹配模板<typename ...>到模板<typename>缺陷?

时间:2017-12-09 17:16:09

标签: c++ c++11 c++14 language-lawyer

在探索this answer时,我发现需要参数包的模板不会被需要具有特定参数数量的模板的模板接受。

在我看来,这是一个缺陷,因为如果模板可以采用任意数量的参数,它应该能够映射到特定的数字。是否有语言律师可以解释为什么不允许这样做?

这是一个简单的例子:

template <typename...Ts>
using pack = void;

template <template <typename> class>
using accept_template = int;

accept_template<pack> value = 0;

我当然不会在这个确切的场景中使用它。它将用于将模板传递给另一个模板,该模板将以某种方式使用传递的模板。在我链接的答案中,我已经说明了一种解决方法,但我仍然认为这是一个缺陷。

1 个答案:

答案 0 :(得分:12)

P0522导致此限制被放宽,这引入了新规则来处理模板模板参数与模板模板参数的匹配方式。结果,来自论文:

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中编译。