【问题标题】:Is not matching a template<typename...> to template<typename> a defect?不将模板<typename...> 与模板<typename> 匹配是缺陷吗?
【发布时间】:2017-12-09 17:16:08
【问题描述】:

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

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

这是一个简单的例子:

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

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

accept_template<pack> value = 0;

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 手边没有。
    • @Adrian 问题中的示例在 C++17 模式下被 GCC 7.2 接受,如果您可以选择使用它。
    • 很高兴发现这是一个缺陷。 :)
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 2010-12-08
    • 2015-05-12
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多