Sprout/sprout/cstdlib/abs.hpp
2012-05-27 00:43:38 +09:00

54 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef SPROUT_CSTDLIB_ABS_HPP
#define SPROUT_CSTDLIB_ABS_HPP
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/type_traits/enabler_if.hpp>
namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk)
// 7.20.6.1 abs<62>Clabs<62>CyÑ llabs ŠÖ<C5A0>
SPROUT_CONSTEXPR int abs(int j) {
return j < 0 ? -j : j;
}
SPROUT_CONSTEXPR long labs(long j) {
return j < 0 ? -j : j;
}
SPROUT_CONSTEXPR long long llabs(long long j) {
return j < 0 ? -j : j;
}
SPROUT_CONSTEXPR long abs(long j) {
return sprout::labs(j);
}
SPROUT_CONSTEXPR long long abs(long long j) {
return sprout::llabs(j);
}
namespace {
template<
typename IntType,
typename sprout::enabler_if<
std::is_integral<IntType>::value && std::is_signed<IntType>::value
>::type = sprout::enabler
>
SPROUT_CONSTEXPR IntType abs(IntType j) {
return j < 0 ? -j : j;
}
template<
typename IntType,
typename sprout::enabler_if<
std::is_integral<IntType>::value && std::is_unsigned<IntType>::value
>::type = sprout::enabler
>
SPROUT_CONSTEXPR IntType abs(IntType j) {
return j;
}
} // anonymous-namespace
} // namespace sprout
#endif // #ifndef SPROUT_CSTDLIB_ABS_HPP