Boost logo

Boost Users :

Subject: Re: [Boost-users] [STL] Is there a std::abs<>() function? (Max)
From: Max (loadcom_at_[hidden])
Date: 2008-12-25 23:34:31


In my opinion, the self-made version in my initial post
is probably superior than yours. :-P

which is repeated as follows:

#include <boost/utility/value_init.hpp>

// my abs function
// it works not only for POD types, but also for
// user types that are default constructible and
// support operator<() and unary operator-()
template<class T> T abs(const T & x)
{
    static boost::value_initialized<T> zero;
    return (x < zero? -x : x);
}

Max

----- Original Message -----
From: Deane Yang <deane.yang_at_[hidden]>
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [STL] Is there a std::abs<>() function? (Max)
Date: 2008-12-25 18:09:26

I just made up my own naive version. Improvements would be welcome:

     template<typename T>
     inline
     T AbsoluteValue(const T& x)
     {
       if (x < T(0))
       {
         return -x;
       }
       else
       {
         return x;
       }
     }


Max wrote:
> seconded.
>
> But could you tell me where I can find the temple version of
> AbsoluteValue function you mentioned?
>
> Thanks
> B/Rgds
> Max
>

-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(http://space.sina.com.cn/ )


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net