std::nullptr_t is an alias for decltype(nullptr). The actual type of nullptr is unnamed. Why is std::nullptr_t an alias to an unnamed type instead of just being a built in type like int or char? That is like having the type int be unnamed and std::int_t being an alias to to decltype(1).
EDIT: This question is not a duplicate for this question, because that question was asking where std::nullptr_t was in the std namespace, and assumed that nullptr_t was a built in type which it is not. This question is simply asking why std::nullptr_t is an alias.
std::nullptr_tis its own type so that it may be implicitly converted to any pointer and pointer to member type. It cannot be a built-in type likeintor evenvoid*because it is an actual type, sonullptris not a macro (unlikeNULL) andintorvoid*are not implicitly convertible to other pointer types.It is technically not named (though I'd say
std::nullptr_tis its name) because it doesn't need to be named, and reserving another keyword likeintorcharjust for that would potentially break existing code. Introducing new keywords is never easy.Example:
It was introduced to fix some issues with the
NULLmacro. Mainly for type safety.One issue with
NULLis ambiguous calls to overloads:Another issue is auto type deduction: