Search code examples
c++c++11templatesnullptr

Can nullptr be used as a type?


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?


Solution

  • 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