Re: Help with g++ 6 abs() and templates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19/02/16 20:18 -0700, Orion Poplawski wrote:
octave has template functions that call abs() on templated variables, something like:

template <class T>
T myfunc(T arg) {
T x = abs(arg);
}


This leads to errors when instantiated for unsigned types because abs(unsigned type) is not defined as is doesn't make sense to do. That's fine and all, but how then does one write generic template code as above that works for signed and unsigned types.


template <class T>
 typename std::enable_if<std::is_signed<T>::value, T>::type
 generic_abs(T arg) {
   return abs(arg);
 }

template <class T>
 typename std::enable_if<!std::is_signed<T>::value, T>::type
 generic_abs(T arg) {
   return arg;
 }

template <class T>
 T myfunc(T arg) {
   T x = generic_abs(arg);
 }
--
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxxx
http://lists.fedoraproject.org/admin/lists/devel@xxxxxxxxxxxxxxxxxxxxxxx




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Fedora Announce]     [Fedora Kernel]     [Fedora Testing]     [Fedora Formulas]     [Fedora PHP Devel]     [Kernel Development]     [Fedora Legacy]     [Fedora Maintainers]     [Fedora Desktop]     [PAM]     [Red Hat Development]     [Gimp]     [Yosemite News]
  Powered by Linux