I am trying to make a template operator function for subtracting a scalar from a vector and return the result in a vector. The function below is what I have been able to come up with:
// vector - scalar
template<typename T1, typename T2>
auto operator - ( const Vector<T1> & lhs, const T2 & scalar )-> Vector<std::common_type_t<T1,T2>>
{
using T3 = std::common_type_t<T1, T2>;
Vector<T3> result;
result.base.reserve( lhs.base.size() );
std::transform( lhs.base.begin(), lhs.base.end(), std::back_inserter( result.base ),
[&scalar]( const T3 & element ) { return element - scalar; } );
return result;
}
However, the result is not correct when trying to subtract a scalar (float) from a vector (int). I suspect the problem is because the operands undergo the usual arithmetic conversions, which include integral promotions.