Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ automatic type casting : wrong behaviour for a container class

I'm implementing some classes for linear algebra operations on very small constant size vector and matrices. Currenty, when I do :

MyMathVector<int, 3> a ={1, 2, 3};
MyMathVector<double, 3> b ={1.3, 2.3, 3.3};
std::cout<<"First = "<<a+b<<std::endl;
std::cout<<"Second = "<<b+a<<std::endl;

Then First = {2, 4, 6} and Second = {2.3, 4.3, 6.3}, because the second element is casted to the first element type by the compiler. Is there any "easy" way to provide the same kind of automatic casting like in native C++ : int+double=double, double+int=double ?

Thank you very much.

EDIT : With the syntax given from answers, I got the operator+ working. But I tried the following syntax, and the compilation fails with the error : expected a type, got ‘std::common_type<T, TRHS>::type’

#include <iostream>
#include <type_traits>

template<class T> class MyClass
{ 
    public:
        MyClass(const T& n) : _n(n) {;}
        template<class TRHS> MyClass<typename std::common_type<T, TRHS>::type> myFunction(const MyClass<TRHS>& rhs) 
        {
            return MyClass<std::common_type<T, TRHS>::type>(_n*2+rhs._n);
        }
        T _n; 
};

int main()
{
    MyClass<double> a(3);
    MyClass<int> b(5);
    std::cout<<(a.myFunction(b))._n<<std::endl;
}

What is the problem of that syntax ?

like image 609
Vincent Avatar asked Jul 31 '12 15:07

Vincent


3 Answers

Use std::common_type:

template <std::size_t s, typename L, typename R>
MyMathVector<typename std::common_type<L, R>::type, s> operator+(MyMathVector<L, s> const& l, MyMathVector<R, s> const& r)
{
    // do addition
}

Ot in case of the member function (in the class body, where T and s are visible):

template <typename TRHS>
MyMathVector<typename std::common_type<T, TRHS>::type, s> operator+(MyMathVector<TRHS, s> const& rhs) const
{
    // do addition
}
like image 90
yuri kilochek Avatar answered Oct 20 '22 22:10

yuri kilochek


Use the std::common_type trait to figure out the correct result type for a mixed operation.

The linked page even has an example that's very similar to your case.

like image 39
Kerrek SB Avatar answered Oct 20 '22 22:10

Kerrek SB


Absolutely; use decltype:

template<typename Other>
auto operator+(const MyMathVector<Other, size> &other)
    -> MyMathVector<decltype(std::declval<T>() + std::declval<Other>()), size>;

As a non-member operator, it might be better to say what you mean by actually referencing a vector member:

template<typename size, typename L, typename R>
auto operator+(const MyMathVector<L, size> &l, const MyMathVector<R, size> &r)
    -> MyMathVector<decltype(l[0] + r[0]), size>;
like image 4
ecatmur Avatar answered Oct 20 '22 22:10

ecatmur