Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The _v template variables 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 template variables 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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading