Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_integral vs is_integer: is one of them redundant?

Tags:

is_integral and is_integer seem to answer the same thing in the same way.

From the links to the related documentation pages, is_integral seems to be missing specializations for the following types

signed char        unsigned char      unsigned short     unsigned int       unsigned long      unsigned long long 

Yet a compiled example, shows (of course) their identical behaviour on those types as well:

#include <iostream> #include <type_traits> using namespace std;  int main()  {     cout << is_integral<signed char       >::value << endl;     cout << is_integral<unsigned char     >::value << endl;     cout << is_integral<unsigned short    >::value << endl;     cout << is_integral<unsigned int      >::value << endl;     cout << is_integral<unsigned long     >::value << endl;      cout << is_integral<unsigned long long>::value << endl;     return 0; } 

So if they also behave the same, what was the point of introducing both of them in C++11?

So if they also behave the same, what was the point of introducing both of them in c++11 ?

Edit : rephrasings

As Useless points out, the phrase including any signed, unsigned, and cv-qualified variants from the is_integral doc page reveals that even their specifications are a complete match.

like image 890
Nikos Athanasiou Avatar asked May 31 '14 13:05

Nikos Athanasiou


People also ask

What is Is_integral?

std::is_integralTrait class that identifies whether T is an integral type. It inherits from integral_constant as being either true_type or false_type, depending on whether T is an integral type: fundamental integral types. bool. char.

What is integral Type C++?

Built-in types (also called fundamental types) are specified by the C++ language standard and are built into the compiler. Built-in types aren't defined in any header file. Built-in types are divided into three main categories: integral, floating-point, and void. Integral types represent whole numbers.


1 Answers

std::numeric_limits<T>::is_integer was not introduced by C++11. It was just updated to use the new constexpr qualifier.

std::is_integral<T> was introduced by C++11, and you're right it gives the same results. As to why it was added - possibly because the integral-ness or otherwise of a type isn't logically part of that type's numeric_limits?

It seems to be the goal of the <type_traits> header to gather all the type classification helpers in one place, while the older numeric_limits collects only properties specific to, well, numbers. If numeric_limits<T>::is_integer were deprecated, there'd be a slightly arbitrary boundary which type traits lived in <type_traits>, and which were considered numerical. It's hardly a terrible duplication to have it in both places.

like image 122
Useless Avatar answered Oct 18 '22 01:10

Useless