Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;模板&x27;的模板未能编译_C++_Templates_Compilation_Constants - Fatal编程技术网

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头版本()with
c++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;
}