Get rid of the warning in the template method due to unauthenticity

I found some boilerplate code that at some point performs the following check:

template<class IntegralType>
void randomFunction(IntegralType t)
{
    ...
    if (t < 0)
    ...
}

The idea of ​​the code is that it thas an integer type (either signed or unsigned). The code works fine, regardless of the subscription, but the compiler generates a warning, because in the case of an integer, the unsignedcheck will always be true.

Is there a way in C ++ 03 to change the code to get rid of the warning without suppressing it? I thought about verifying the signature in tsome way, I don’t know that this is possible.

I know C ++ 11 is_signed, but I'm not sure how this can be implemented in C ++ 03.

+4
source share
3

Tag:

template <typename T>
bool is_negative(T t, std::true_type)
{
    return t < 0;
}
template <typename T>
bool is_negative(T t, std::false_type)
{
    return false;
}

template<class IntegralType>
void randomFunction(IntegralType t)
{
    ...
    if (is_negative(t, std::is_signed<IntegralType>::type())
    ...
}

std::is_signed ++ 03.

+6

++ 11 is_signed cppreference :

namespace detail {
template<typename T,bool = std::is_arithmetic<T>::value>
struct is_signed : std::integral_constant<bool, T(-1) < T(0)> {};

template<typename T>
struct is_signed<T,false> : std::false_type {};
} // namespace detail

template<typename T>
struct is_signed : detail::is_signed<T>::type {};

, is_integral_constant ++ 11, ++ 03.

+3

.

, , , :

    if (!(t == 0 || t > 0))

which may be related to the compiler, but at least in g ++ 4.4.7 the warning disappears.

+1
source

Source: https://habr.com/ru/post/1690965/


All Articles