C++ C++;模板&x27;的模板未能编译
我有这个测试程序C++ C++;模板&x27;的模板未能编译,c++,templates,compilation,constants,C++,Templates,Compilation,Constants,我有这个测试程序 #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&
#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;
}
#包括
#包括
使用名称空间std;
模板
无效打印(常量C&C){
对于(auto&e:c)cout您的编译问题是因为您的c
与std::vector
的声明不匹配:
template<
class T,
class Allocator = std::allocator<T>
> class vector;
在这里,您指定了正确的模板签名类型(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;
}
模板
无效打印(常量C&C){
for(auto&e:c)cout包含两个模板参数,而模板参数c
声明为仅包含一个。无论如何,您的代码可以与c++17配合使用;由于c++17(),a允许使用默认模板参数来匹配具有较少模板参数的模板参数
在C++17之前,您可以申请
模板
无效打印(常量C&C){
用于(自动和电气:c)你可能不需要额外的模板
规范,向量
已经完全指定并且足够了。这不是一个重复,因为它没有解释手头的问题。OP,问题是std::vector
实际上是std::vector
。这个问题可以通过简单地使用模板
轻松解决。在这里使用模板模板参数没有任何好处。如果您决定重新打开我的DUPE闭包,请考虑写一个专门解决问题的答案。IMO复制品足以回答这个特定问题。第一个版本在CLAN60.0上失败,C++ 17用于一些。reason@SkepticalEmpiricist头版本()withc++2a
模式也不起作用。似乎clang不支持该功能。
template<
class T,
class Allocator = std::allocator<T>
> class vector;
template<template<class, class> class C, typename T, typename A>
void print(const C<T, A>& c){
for(auto& e : c)cout<<e<<endl;
}
template<template<class...> class C, typename T>
void print(const C<T>& c){
for(auto& e : c)cout<<e<<endl;
}
template<typename T>
void print(const T& c){
for(auto& e : c)cout<<e<<endl;
}
template<template<class...> class C, typename T>
void print(const C<T>& c){
for(auto& e : c)cout<<e<<endl;
}