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

前端 未结 3 1257
无人及你
无人及你 2021-02-05 07:21

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

template 
myClass operator+(myClass

        
3条回答
  •  悲哀的现实
    2021-02-05 07:32

    The compiler on overload resolution fails to find a right candidate for operator+ because T is already being deducted to double and literal 5 is an integer. Solution:

    template 
    myClass operator+(myClass lhs, const T2& rhs) {
        return lhs += T1(rhs);
    }
    

提交回复
热议问题