c++11 如何将遗留 class 转换为模板
c++11 how to convert legacy class to template
我有一个遗产 class 比如:
class Wrapper{
public:
Wrapper(void*);
void* get();
};
我想创建一个类型安全的包装器,例如:
template<class T>
class Wrapper{
public:
Wrapper(T);
T get();
};
由于 C++11,这样的东西将无法工作:
template<class T = void*> //Here I would need <>
class Wrapper...
typedef Wrapper<void*> Wrapper; //This isn't allowed
有没有办法将 Wrapper 转换为模板 class 而无需编辑所有已使用它的地方?
如果你不想在其他地方改变,你可以给你的模板化 class 一个不同的名字(因为你甚至根本不使用它):
template<typename T>
class WrapperT
{
public:
WrapperT(T t) : _T(t) {}
T get() { return _T; }
private:
T _T;
};
using Wrapper = WrapperT<void*>;
如果您随后删除了 Wrapper
的所有用法,您可以重命名 WrapperT
我有一个遗产 class 比如:
class Wrapper{
public:
Wrapper(void*);
void* get();
};
我想创建一个类型安全的包装器,例如:
template<class T>
class Wrapper{
public:
Wrapper(T);
T get();
};
由于 C++11,这样的东西将无法工作:
template<class T = void*> //Here I would need <>
class Wrapper...
typedef Wrapper<void*> Wrapper; //This isn't allowed
有没有办法将 Wrapper 转换为模板 class 而无需编辑所有已使用它的地方?
如果你不想在其他地方改变,你可以给你的模板化 class 一个不同的名字(因为你甚至根本不使用它):
template<typename T>
class WrapperT
{
public:
WrapperT(T t) : _T(t) {}
T get() { return _T; }
private:
T _T;
};
using Wrapper = WrapperT<void*>;
如果您随后删除了 Wrapper
的所有用法,您可以重命名 WrapperT