Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is std::is_integral implemented?

I am not familiar with Template magic in cpp. After reading what 'TemplateRex' said in this link, I was confused about how std::is_intergral works.

template< class T >
struct is_integral
{
    static const bool value /* = true if T is integral, false otherwise */;
    typedef std::integral_constant<bool, value> type;
};

I can understand how SFINAE works and how traits works. After refering cppreference, implementation of 'is_pointer" was found instead of 'is_integral' which looks like this :

template< class T > struct is_pointer_helper     : std::false_type {};
template< class T > struct is_pointer_helper<T*> : std::true_type {};
template< class T > struct is_pointer : is_pointer_helper<typename std::remove_cv<T>::type> {};

Do 'is_integral' have similar implementation? How?

like image 995
yui_frank Avatar asked Apr 23 '17 13:04

yui_frank


1 Answers

From here we have that:

Checks whether T is an integral type. Provides the member constant value which is equal to true, if T is the type bool, char, char16_t, char32_t, wchar_t, short, int, long, long long, or any implementation-defined extended integer types, including any signed, unsigned, and cv-qualified variants. Otherwise, value is equal to false.

Something like this is probably along the way you can implement it:

template<typename> struct is_integral_base: std::false_type {};

template<> struct is_integral_base<bool>: std::true_type {};
template<> struct is_integral_base<int>: std::true_type {};
template<> struct is_integral_base<short>: std::true_type {};

template<typename T> struct is_integral: is_integral_base<std::remove_cv_t<T>> {};

// ...

Note that std::false_type and std::true_type are specializations of std::integral_constant. See here for further details.

like image 60
skypjack Avatar answered Sep 25 '22 08:09

skypjack