如何在不破坏现有客户端代码的情况下将类转换为模板类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>;