0

I was learning about the usage of enable_if and I stumbled upon the following code.

template <class T,
         typename std::enable_if<std::is_integral<T>::value,
                                 T>::type* = nullptr>
void do_stuff(T& t) {
  std::cout << "do_stuff integral\n";
    // an implementation for integral types (int, char, unsigned, etc.)
}

The thing that bothers me is that in the template parameter, nullptr is used as a default parameter to the std::enable_if<std::is_integral<T>::value, T>::type* which is also a type.

I am not sure how we can assign a literal to the type. Shouldn't it be nullptr_t instead?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Jaebum
  • 1,397
  • 1
  • 13
  • 33

2 Answers2

4

This template accept a non-type second parameter, that is a pointer typename std::enable_if<std::is_integral<T>::value, T>::type * so nullptr is used as default value for this pointer. Note that typename in this second parameter is used to make compiler figure out that ::type as a type, it is not a beginning of usual type template parameter like typename T

user7860670
  • 35,849
  • 4
  • 58
  • 84
1

nullptr is not a type, it's a value (of type nullptr_t, which can be converted to any pointer type IIRC). Otherwise, any standard usage of nullptr like:

int* a = nullptr;

would not work.

This is an unnamed default template parameter used to allow SFINAE in a template declaration instead of using the return type. It's basically like:

template<int=0>
void foo();

With the SFINAE trick/enable_if.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62