jeudi 16 septembre 2021

Is there a way to call a template constructor from a specialized constructor?

Lets say I have this class:

template <class T>
class Test
{
    Test(T* x);

    const T* const t;
    int i{0};
};

I want t to always be initialized with x:

template <class T> Test<T>::Test(T* x) : t{x} {}

And I have two specializations:

template <> Test<Foo>::Test(Foo* x) : t{x} { i = 1; }
template <> Test<Bar>::Test(Bar* x) : t{x} { i = 2; }

Next, I'm extending the class with some other stuff, and that first (templated) constructor does a lot more than just setting t.

All things that I want to do for both T = Foo and T = Bar.

Is there some way that I can call the templated constructor from the specialized constructors?

//This does not work, since it will create a delegation cycle
template <> Test<Foo>::Test(Foo* x) : Test(x) { i = 1; }
template <> Test<Bar>::Test(Bar* x) : Test(x) { i = 2; }

Aucun commentaire:

Enregistrer un commentaire