I have this working on G++ but on Visual Studo 2008 this will not compile.
template<typename T, typename DerivedT >
struct Foo
{
template<typename Scale>
DerivedT operator * (const Scale i)
{
DerivedT result;
return result;
}
};
template<typename T>
struct Bar : public Foo<T, Bar<T> >
{
// Removing this operator gets rid of the error.
Bar& operator * (const Bar& boo)
{
return *this;
}
};
int main()
{
Bar<float> bar;
bar = bar * 3;
return 0;
}
I get the error
Error 1 error C2679: binary '*' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Even if I define the Foo operator as an int/double/float explicitly it returns the same error message. Is there any way of getting past this?
EDIT:
This only falls apart when the derived class overload the operator * that is also defined in the Base class.
I don’t know how you managed to compile this by g++ (and I actually doubt that), but your code is indeed not compilable for rather obvious reasons. Your
Barclass exposes only oneoperator *and that operator expects a
Barobject as a right-hand size operand.3will not work,3is notBarand is not convertible toBar.The base class’s
operator *is the one that might have worked here, but it is hidden by the derived class’s operator. This is why, as one would expect, removing the derived class’soperator *gets rid of the error.You can simply add the
to the definition of
Barto unhide the base class’s operator and it should compile.