I have a class hierarchy with three classes (A, B and C). A and B are base-classes, parametrized with the derived Type. Class C is derived from both, A and B.
The class B provides an assignment operator for objects of type A and class C inherits this assignment operator with the using super::operator=
declaration.
When I define a constructor in class B from objects of type A, I get the Error: two overloads have similar conversions (C2666) in Visual Studio 2013, but I don't get any error, or warning in gcc (4.8.2), clang (3.4) and intel icc (Studio 2015). (compiled with -Wall -pedantic
)
Here the reduced example:
template <class Model> struct A {};
template <class Model> struct B
{
B() {}; // default constructor
// copy constructor for objects of type A
template <class M>
B(A<M> const&) {}
// assignment operator for objects of type A
template <class M>
Model& operator=(A<M> const& rhs)
{
return static_cast<Model&>(*this);
}
};
struct C : public B<C>, public A<C>
{
typedef B<C> super;
// copy assignment operator
C& operator=(C const& rhs) { return *this; }
// adopt assignment operator for A<C> from super-class
using super::operator=;
};
int main()
{
C c;
A<C> a;
c = a;
}
If I would replace the templated class A by a non-templated class it also compiles in Visual Studio without errors - but this is not the way it could be solved.
My question is: is this construct well-formed in the sense that it is standard conform, or is the error-message correct? Does a specifier like explicit
for the copy constructor in B helps to solve the problem?
By the way: In Visual Studio, I get the Warning: multiple assignment operators specified (C4522), because of the copy assignment operator in class C. Can somebody exmplain to me, why this should be a problem?