13

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?

Community
  • 1
  • 1

1 Answers1

15

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.

L. F.
  • 19,445
  • 8
  • 48
  • 82
skypjack
  • 49,335
  • 19
  • 95
  • 187
  • You also need to remove the cv-qualifiers, since `is_integral` needs to return true for them. As well as handle signed/unsigned. – Nicol Bolas Apr 23 '17 at 14:10
  • @NicolBolas Right, let me update the answer. Thank you. – skypjack Apr 23 '17 at 14:11
  • 1
    specializations of **`std::integral_constant`** – DutChen18 Jan 19 '18 at 22:05
  • @skypjack What about implementation-defined *extended integer types*? – L. F. Feb 20 '19 at 10:04
  • @L.F. what's the question exactly? – skypjack Feb 20 '19 at 10:05
  • @skypjack Your implementation cannot handle them properly and portably. – L. F. Feb 20 '19 at 10:06
  • @L.F. those types are implementation defined. With a bunch of `ifdef` and a few more specializations, you can handle them as well. I don't see the problem. – skypjack Feb 20 '19 at 10:10
  • @skypjack That can work. But can you provide an implementation that works for all implementations? (I mean, w/o exhausting each implementation with `#ifdef` or related; for example, can it work on an environment rolled _ad hoc_ by myself) – L. F. Feb 20 '19 at 10:12
  • @L.F. SO is a Q/A service, not a _do it for me and for free_ service. I provided an answer that explains how to do it, I commented my own answer to tell how to do it also for implementation defined types. I won't write a full implementation that works in all cases, I'm sorry. This isn't even the purpose of answering on SO to be honest. – skypjack Feb 20 '19 at 10:18
  • @skypjack Calm down dude, anger is a disaster to mental health :) Your answer is ok, but would be better if your answer includes a (good) portable way that functions on all environments. I never asked you to do it for "me" for free; sorry if I fail to be taught to remember to by default put a disclaimer saying "this is not for me" when adding a comment :) Anyway, the point is I want to help **improve** your answer, and I respect you if you resist improvement anyway. If that is the case, there are a lot of people who don't like improving, you are not alone. – L. F. Feb 20 '19 at 10:27
  • 1
    @L.F. I'm calm, but I don't see how this improvement can help. The question was _How is std::is_integral implemented?_ and here it is an answer that takes in consideration a few types to demonstrate it. You could argue that I didn't consider `wchar_t` as well, but adding it won't add anything to the answer itself. My two cents. – skypjack Feb 20 '19 at 10:36
  • @skypjack Actually I was thinking of something like using `intmax_t` and using some strategy to allow only standard conversions whereas preventing narrowing conversions to test for integral types, including extended ones. – L. F. Feb 20 '19 at 10:38
  • @L.F. I cannot figure it out right now, but ping me if it turns in actual code. I'm just curious now. – skypjack Feb 20 '19 at 10:40