samedi 25 avril 2020

C++ std::is_integral on lvalue and rvalue shows different results

I created a type validator to check whether a given argument is numeric.

template<typename T>
struct is_numeric
    : std::integral_constant<
        bool,
        std::is_integral_v<T> || std::is_floating_point_v<T>
    > {};

template<typename T>
inline constexpr bool is_numeric_v = is_numeric<T>::value;

template<typename T>
constexpr bool is_numeric_tuple(T&& value)
{ return is_numeric_v<T>; }

// variadic template implementation is omitted

Now, the problem is that

int i = 3;
is_numeric_tuple(3)  // returns true
is_numeric_tuple(i)  // returns false

And if I apply std::remove_reference to is_numeric_tuple, both results turn out to be true.

Does it mean that the STL implementation of type_traits like is_integral, is_floating_point, etc enforces given type to be rvalue?

And if so, why?

Aucun commentaire:

Enregistrer un commentaire