Boost logo

Boost Users :

Subject: [Boost-users] [STL] Is there a std::abs<>() function?
From: Max (loadcom_at_[hidden])
Date: 2008-12-21 19:16:17


Hello,

It's strange to me that there seems not to be a std::abs<>() function
in STL, with it I can write code like this:

double d = std::abs(-1.0);
int i = std::abs(-1);

Does anybody else have the same feeling?

Being uncomfortable with the abcence of it, I've wrote my own version
in my namespace:

#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);
}

Thanks for any information.

Best regards
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