C++ Template problem adding two data types

后端 未结 9 1488
慢半拍i
慢半拍i 2021-02-13 02:51

I have a template class with an overloaded + operator. This is working fine when I am adding two ints or two doubles. How do I get it to add and int and a double and return th

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 03:25

    Get a compiler that supports the new C++0x decltype operator.

    template < typename T1, typename T2 >
    auto add(T1 t1, T2 t2) -> decltype(t1+t2)
    {
      return t1 + t2;
    }
    

    Now you don't have to fart around with those "traits" classes.

提交回复
热议问题