samedi 5 juin 2021

How to turn a class into a template class without breaking existing client code (pre c++17)?

Can I turn a class into a template class without breaking user code? I am trying to change a class to accept template parameters, but at the same time, I would like to avoid breaking existing client code. In more detail, the existing codebase was

class A{ // some code };

I turned this into the following:

template <type T = defaultType>
class A{ // some code };

The non-templatized code was effectively written for defaultType. Can I do anything so that existing code does not break when the code is compiled without c++17 (or greater) support? For example, the following should be valid:

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;

I am grateful for any hints or suggestions!

Aucun commentaire:

Enregistrer un commentaire