Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) ?

like image 596
Vincent Avatar asked Mar 03 '13 03:03

Vincent


People also ask

What is c++ type deduction?

Type inference or deduction refers to the automatic detection of the data type of an expression in a programming language. It is a feature present in some strongly statically typed languages. In C++, the auto keyword(added in C++ 11) is used for automatic type deduction.

What is Nullptr used for in C++?

The nullptr keyword can be used to test if a pointer or handle reference is null before the reference is used. Function calls among languages that use null pointer values for error checking should be interpreted correctly. You cannot initialize a handle to zero; only nullptr can be used.


1 Answers

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 *.

like image 104
chris Avatar answered Sep 18 '22 09:09

chris