Home > database >  c 11 how to convert legacy class to template
c 11 how to convert legacy class to template

Time:09-29

I have a legacy class like:

class Wrapper{
public:
    Wrapper(void*);
    void* get();
};

I want to create a type safe wrapper like:

template<class T>
class Wrapper{
public:
    Wrapper(T);
    T get();
};

Stuff like this won't work because of C 11:

template<class T = void*> //Here I would need <>
class Wrapper...

typedef Wrapper<void*> Wrapper; //This isn't allowed

Is there a way to convert Wrapper to a template class without editing all places where it's already used?

CodePudding user response:

If you do not want to change at other places, you can give your templated class a different name (because you don't even use it then in the first place):

template<typename T>
class WrapperT
{
public:
    WrapperT(T t) : _T(t) {}
    T get() { return _T; }
private:
    T _T;
};

using Wrapper = WrapperT<void*>;

If you then removed every usage of Wrapper you can rename WrapperT

  • Related