既然std::string实际上是typedef模板类,我怎么能覆盖呢?我想制作一个std::string能返回正确长度的UTF-8 .
看看这段代码.
#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.