If I have a generic struct/class:
template<typename T>
struct Container
{
T value;
Container(const Value& value) : value(value) { }
};
And I want to perform an operation on two of them:
template<typename T, typename U>
Container<T> operator+(const Container<T>& lhs, const Container<U>& rhs)
{
return Container<T>(lhs.value + rhs.value);
}
The problem is that if lhs
is of the type Container<int>
and rhs
is of the type Container<float>
, then I'll get a Container<int>
back. But if I were to do auto result = 2 + 2.0f
, then result
would of of type float
. So the behavior is inconsistent between builtin types and custom types.
So how would I take the operator+
overload and make it return Container<float>
, much like how C++ handles arithmetic promotion with builtin types?