ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C模板的模板无法编译

2019-07-27 02:09:09  阅读:217  来源: 互联网

标签:c templates compilation const


我有这个测试程序

#include<iostream>
#include<vector>
using namespace std;

template<template<class> class C, typename T>
void print(const C<T>& c){
    for(auto& e : c)cout<<e<<endl;
}
int main(){
    vector<int> v;
    print(v);
    return 0;
}

它无法编译:

g++ m.cpp -std=c++11
m.cpp: In function ‘int main()’:
m.cpp:11:16: error: no matching function for call to ‘print(std::vector<int>&)’
        print(v);
                ^
m.cpp:6:6: note: candidate: template<template<class> class C, class T> void print(const C<T>&)
void print(const C<T>& c){
    ^~~~~
m.cpp:6:6: note:   template argument deduction/substitution failed:
m.cpp:11:16: note:   template parameters of a template template argument are inconsistent with other deduced template arguments
        print(v);
                ^

我将print()签名从(const C& c)更改为(C& c),它仍然失败:

$g++ m.cpp -std=c++11
m.cpp: In function ‘int main()’:
m.cpp:11:16: error: no matching function for call to ‘print(std::vector<int>&)’
        print(v);
                ^
m.cpp:6:6: note: candidate: template<template<class> class C, class T> void print(C<T>&)
void print(C<T>& c){
    ^~~~~
m.cpp:6:6: note:   template argument deduction/substitution failed:
m.cpp:11:16: note:   template parameters of a template template argument are inconsistent with other deduced template arguments
        print(v);
                ^

怎么解决?

解决方法:

您的编译问题出现是因为您的template template parameter C与std :: vector的声明不匹配:

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

如您所见,std :: vector有两个模板参数,而您的C只有一个.但是,请注意第二个参数(类Allocator)具有默认类型参数.从C17开始,即使你编写它也是很好的形式,因为添加模板模板参数匹配不需要为具有默认参数(如Allocator)的参数指定参数.但并非所有编译器都支持对语言规范的修改 – 请参阅here live how Clang 6.0.0 refuses以编译启用了C 17的原始代码段.寻找旧版本的C(或者只是Clang的任何版本),这个片段可能就是你的目标:

template<template<class, class> class C, typename T, typename A>
void print(const C<T, A>& c){
    for(auto& e : c)cout<<e<<endl;
}

在这里,您指定类型(std :: vector)的正确模板签名,您稍后将使用实例化print().

无论C 17如何,这都会有效:

template<template<class...> class C, typename T>
void print(const C<T>& c){
    for(auto& e : c)cout<<e<<endl;
}

也就是说,请注意,作为vector< int>已经是完全实例化的类型,这个更简单的版本在您的代码段的给定范围内也可以正常工作:

template<typename T>
void print(const T& c){
    for(auto& e : c)cout<<e<<endl;
}

I changed print() signature from (const C& c) to (C& c), it still
fails:

在这种情况下,这可能是更好的做法,因为你没有修改print()中的c.但是,这与您的错误无关.

标签:c,templates,compilation,const
来源: https://codeday.me/bug/20190727/1549318.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有