I want to create a family of template classes, where each of them would derive from common base. This common base would define, how to convert instance of one of these derived classes to any other. I have created base class template with both copy constructor and copy assignment operator, here is the code:
template < class T >
struct class_family
{
T data;
class_family() = default;
class_family(const class_family& a) : data(a.data) { }
class_family& operator=(const class_family& a) { data = a.data; return *this; };
};
Then, I created 2 derived classes, which derive from this base class, so that I can avoid to duplicate code for copy construction and copy assignment for each possible combination of derived classes:
template < class T >
struct class_1
: public class_family< T >
{
using class_family< T >::class_family;
using class_family< T >::operator=;
};
template < class T >
struct class_2
: public class_family< T >
{
using class_family< T >::class_family;
using class_family< T >::operator=;
};
Here is my test code:
int main(int argc, char** argv)
{
class_1<int> c1;
class_2<int> c2;
c1 = c2;
// class_2<int> c3 = c1;
}
When it comes to copy assignment, everything works as intended, however when I uncomment line where I try to initialize new object of type class_2
with object of type class_1
, compiler complains with following error:
E0312 no suitable user-defined conversion from "class_1<int>" to "class_2<int>" exists
Shouldn't compiler see inherited copy constructor from base class? Is there some workaround to avoid duplicating copy constructor for every single class belonging to class_family
?