Search code examples
c++templatesc++11nullptr

Pointed template type deduced from a nullptr?


Considering the function :

template <class T> void f(const T* const ptr);

What is T for f(nullptr) ?


Solution

  • I would have to answer this with there is none. From § 2.14.7/1 (emphasis mine):

    The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.

    T * would have to be std::nullptr_t and since std::nullptr_t is not a pointer type, that isn't possible. Trying to call it with nullptr on GCC 4.7.2 gives an error indicating that it was trying to call f(std::nullptr_t), but only had f(const T *), which agrees with the fact that a std::nullptr_t is not a T *.