如何在不破坏现有客户端代码的情况下将类转换为模板类(c++17之前)?
我可以在不破坏用户代码的情况下将类转换为模板类吗?我正在尝试更改一个类以接受模板参数,但同时,我想避免破坏现有的客户端代码。更详细地说,现有的代码库是
class A{ // some code };
我把它变成了以下内容:
template <type T = defaultType>
class A{ // some code };
非模板化代码是为defaultType. 我可以做任何事情以使现有代码在没有 c++17(或更高版本)支持的情况下编译时不会中断吗?例如,以下内容应该是有效的:
A a{}; //existing code; works under c++17 with CTAD. How to use in c++14?
A<typeFoo> b{} //new code, if users decide to use a different template argument;
我很感激任何提示或建议!
回答
您可以执行以下std::string操作:
template <typename T>
class basic_A{ ... };
using A = basic_A<defaultType>;
- Thank you so much, @Caleth! You accidentally also helped me understand `basic_string`.