将std :: vector重命名为另一个类以重载? [英] Rename std::vector to another class for overloading?
本文介绍了将std :: vector重命名为另一个类以重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看此代码。
#include <vector>
template<class ...Args>
using other_vector = std::vector<Args...>;
template<class T>
void f(std::vector<T>& ) {}
template<class T>
void f(other_vector<T>& ) {}
int main()
{
other_vector<int> b;
f(b);
return 0;
}
它不编译,因为 f
正在重新声明。我完全理解错误。但是,我需要一个类似 std :: vector< T>
的第二个类,但将被视为一个不同的类型,因此重载,
It does not compile, because f
is being redeclared. I totally understand the error. However, I need a second class that behaves like std::vector<T>
, but will be seen as a different type, so that overloading, like in the above example, would be legal.
我可以做什么?
- 让新类具有
std :: vector< T>
作为基类。这可能会工作,但不应继承std容器。 - 让新类有一个类型为std :: vector的成员,然后redeclare所有函数重定向到成员的函数。听起来很多工作。
- Let the new class have
std::vector<T>
as a base class. This might work, but one should not inherit from std containers. - Let the new class have a member of type std::vector and then redeclare all functions to redirect to the functions of the member. Sounds like a lot of work.
更好的选择吗?
推荐答案
您可能会试图混淆分配器:
You might try to mess with the allocator:
template<class T>
struct allocator_wrapper : T { using T::T; };
template<class T, class A = std::allocator<T>>
using other_vector = std::vector<T, allocator_wrapper<A>>;
这篇关于将std :: vector重命名为另一个类以重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文