c++c++14type-traits

Using is_XXX_v<> in C++14


I'm trying to port a C++17 project to an exotic platform that doesn't have anything newer than C++14. The C++17 project I'm trying to port to C++14 uses a lot of calls to is_XXX_v functions, e.g. is_integral_v, is_floating_point_v and is_same_v, which are unavailable in C++14.

My C++ knowledge is very limited so I'm wondering if it's easily possible to emulate those is_XXX_v calls in C++14 or does that require a major rewrite of the code?


Solution

  • The _v variable templates that were added to the traits in C++17 are just syntactic sugar for using their ::value member.

    As you can see in the documentation (1,2,3) this is the way the _v variable templates are defined:

    template< class T >
    inline constexpr bool is_integral_v = is_integral<T>::value;
    template< class T >
    inline constexpr bool is_floating_point_v = is_floating_point<T>::value;
    template< class T, class U >
    inline constexpr bool is_same_v = is_same<T, U>::value;
    

    Therefore you can simply use:

    is_integral<T>::value         // for `is_integral_v`
    is_floating_point<T>::value   // for `is_floating_point_v`
    is_same<T, U>::value          // for `is_same_v`
    

    They are all available since C++11.

    A side note:
    It would be the simplest if you could add the definitions above (for is_integral_v etc.) to your code base, but this will not work because:

    1. inline variables are from C++17.
    2. I assume you are using the std:: prefix (as good practice dictates) and you shouldn't add definitions to namespace std (except template specializations) - see here.