lundi 5 octobre 2015

Why doesn't my templated function promote 'int' to 'T', where 'T' = 'double'

I have a class templated with typename T. It contains a function

template <typename T, size_t a>
myClass<T,a> operator+(myClass<T,a> lhs, const T& rhs) {
    return lhs += rhs;
}

myClass<T,a> myClass<T,a>::operator+=(const T& rhs) {
    // do addition, depends on a
    return *this;
}

When I call this with, for example

myClass<double, 2> myObj_double_2(constructor args);
myObj_double_2 = myObj_double_2 + 5.2;

I have no problem.

If I however call

myObj_double_2 = myObj_double_2 + 5;

Then the compiler gives me a message like - No match for 'operator+' (operand types are 'myClass<double, 2ul>' and 'int'). Candidates are ... note: deduced conflicting types for parameter 'const T' ('double' and 'int')

Can I write the code in some way to allow additional types to be passed that have a conversion to T? (since e.g. double(5) is a valid constructor call)

Aucun commentaire:

Enregistrer un commentaire