/*
partial ordering of function templates
*/
#include <iostream>
using namespace std;
class alloc
{
};
template<class T, class Alloc=alloc>
class vector
{
public:
void swap(vector<T, Alloc>&)
{
cout<<"swap()"<<endl;
}
};
#ifdef _STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class T, class Alloc>
inline void swap(vector<T, Alloc>& x, vector<T, Alloc>& y)
{
x.swap(y);
}
#endif
int main()
{
vector<int> x;
vector<int> y;
swap(x, y);
system("pause");
return 0;
}STL源码剖析:partial ordering of function templates
最新推荐文章于 2024-12-18 14:00:31 发布
本文展示了一个使用C++模板特化的例子,并实现了一个通用的swap函数来交换两个vector对象的内容。通过定义一个alloc类和一个接受类型参数T及可选分配器参数的vector类,演示了如何使用模板特化来提供特定类型的swap实现。
268

被折叠的 条评论
为什么被折叠?



