【发布时间】:2021-11-21 03:06:14
【问题描述】:
我有一个遗留类,例如:
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 转换为模板类,而无需编辑所有已使用的地方?
【问题讨论】:
-
在 C++17 中,CTAD 允许在某些地方省略
<..>。 -
当您对代码进行现代化改造时,您可能会使用
using而不是typedef:using Wrapper = WrapperT<void*>;。
标签: c++ c++11 templates typesafe