可变参数模板是否可以匹配非可变参数模板参数?

 似水年华的梦想_818 发布于 2023-02-13 10:32

请考虑以下代码段:

template class T,class U>
struct apply
{
     typedef T type;
};

typedef apply::type tuple_of_one_int; //Should be std::tuple

GCC 4.8.2.说:

type/value mismatch at argument 1 in template parameter list for [...] struct apply
expected a template of type ‘template class T’, got ‘template class std::tuple’

这基本上意味着类std::tuple变量模板不是Tin 的有效模板参数apply.

这是一个GCC错误还是标准要求这种行为?

1 个回答
  • 有人纠正我,如果我错了,但从这句话看起来似乎是正确的:

    3当template-argument的相应类模板或[FI 11]模板别名模板(称为A)的template-parameter-list中的每个模板参数时,template-argument与模板模板参数(称为P)匹配匹配P的template-parameter-list中的相应模板参数

    A(给定的模板)必须将每个模板参数P与模板模板相匹配.

    从本节的第二部分我们了解到限制不适用于反向,这意味着包含参数包的模板模板可以匹配任何内容.

    当P的template-parameter-list包含模板参数包(14.5.3)时,模板参数包将匹配A的模板参数列表中的零个或多个模板参数或模板参数包,其类型和形式与P中的模板参数包

    你可能已经知道让它工作的方法是

    template<template<class> class T,class U>                                       
    struct apply                                                                       
    {                                                                                  
             typedef T<U> type;                                                        
    };                                                                                 
    
    template<class T> using tuple_type  = std::tuple<T>;
    typedef apply<tuple_type,int>::type tuple_of_one_int;                          
    

    c ++ 11标准也有一个与你相同的例子.

    template <class ... Types> class C { /? ... ?/ };
    
    template<template<class> class P> class X { /? ... ?/ };
    
    X<C> xc; //ill-formed: a template parameter pack does not match a template parameter  
    

    最后的评论完全描述了你的情况,在这种情况下,类C将相当于std::tuple.

    2023-02-13 10:35 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有