Joh*_*nes 6 c++ stdvector c++11
看看这段代码.
#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;
}
Run Code Online (Sandbox Code Playgroud)
它没有编译,因为f正在重新声明.我完全理解错误.但是,我需要一个行为类似的第二个类std::vector<T>,但将被视为一个不同的类型,因此重载,如上例所示,是合法的.
我能做什么?
std::vector<T>作为基类.这可能有效,但不应该从std容器继承.有更好的选择吗?允许使用C++ 11或C++ 14.
Dan*_*rey 17
你可能会试图弄乱分配器:
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>>;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
474 次 |
| 最近记录: |