On this page
nullptr
Prev: Lambdas | Up: builtins | Next: Structured Bindings
Rel: References & Rvalues, std::unique_ptr
Ref: cppreference — nullptr
Tags: #public #prog #cpp #builtins #tier/core
Overview
nullptr (C++11) is the type-safe null pointer literal of type std::nullptr_t. It implicitly converts to any pointer or pointer-to-member type, but never to int—eliminating overload ambiguity caused by C's NULL macro (0 or 0L).
Notes & Pitfalls
- Always use
nullptrinstead ofNULLor0:NULLis an integer literal (0or0L) and callsvoid f(int)instead ofvoid f(int*)in overloaded functions. - Type is
std::nullptr_t: Implicitly converts to any pointer type, never toint.
Example
#include <cassert>
int pick(int) { return 1; }
int pick(int*) { return 2; }
int main() {
int* p = nullptr;
assert(p == nullptr && pick(nullptr) == 2);
}
Linked from
Related notes
- tuts/cpp/builtins/lambdas Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/references_and_rvalues Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/structured_bindings Linked here; links to this page; shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins Linked here; links to this page; shared tags: #builtins, #cpp, #prog
- tuts/cpp/stdlib/memory/unique_ptr Linked here; shared tags: #cpp, #prog, #tier
- tuts/cpp/builtins/auto shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/c_style_casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/casts shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/classes_and_virtual shared tags: #builtins, #cpp, #prog, #tier
- tuts/cpp/builtins/concepts shared tags: #builtins, #cpp, #prog, #tier