How to make a template overload the copy constructor?

81 Views Asked by At

While trying to use a template function as Copy constructor, (how can i do that) i only come up with 3 solutions:

Struct A:

template <typename D> A(const D&) {};

A(A& f)  : A(const_cast<const A&>(f)  {};

But THEN if i inherit from this function, they will have a non-const-reference default copy constructor.

The opposite:

template <typename D> A(D&) {};

A(const A& f)  : A(const_cast<A&>(f)  {};

would avoid the static checking on const-correctness.

I tryied with template <typename D, int> A(D&) {}; but i don't know HOW to parametrice a constructor, so <typename D> A(D&, int e=0) {}; came to mind. But that would be unnaceptable, as its the caller compiled code responsibility to add that 0 defaulted parameter. <-- This is another question, how do you parametrize the constructor without template parameter deduction?

I thought of encasulating it in a (constexpr) class, but too much polution for such a simple thing? .

Please, note, that making a separate function doesn't work because it missed initializers. And i want to change the copy constructor by changing the template, in a possibly different file.

0

There are 0 best solutions below