【发布时间】:2021-02-17 11:13:32
【问题描述】:
这几天我一直在尝试了解模板模板参数。我尝试在 Visual Studio 中使用 C++17 编写示例,但出现此错误:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::vector<int,std::allocator<int>>' (or there is no acceptable conversion)
对于这个例子,我尝试使用来自 STL 的通用容器来突出 C++ 中模板模板参数的概念。
#include <iostream>
#include <vector>
using namespace std;
template<typename T, template<typename> typename Tpl_Type>
ostream& operator <<(ostream& out,const Tpl_Type<T>& x) {
for (auto& aux : x)
out << aux << " ";
out << endl;
return out;
}
template<typename T, template<typename>typename Tpl_Type>
void functioon()
{
Tpl_Type<T> vect2;
for (auto i = 0; i < 5; i++) {
vect2.push_back(i);
}
std::cout << vect2 << endl;
return;
}
int main()
{
functioon<int, std::vector>();
return 0;
}
我做错了什么?
【问题讨论】:
标签: c++ templates parameters operator-overloading operators