Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/nullptr"

From cppreference.com
< cpp‎ | language
m (r2.7.3) (Robot: Adding zh:cpp/language/nullptr)
m (Undo revision 174683 by Benio (talk))
 
(24 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{title|nullptr pointer literal}}
+
{{title|{{tt|nullptr}}, the pointer literal {{mark since c++11}}}}
{{cpp/language/sidebar}}
+
{{cpp/language/expressions/navbar}}
  
 
===Syntax===
 
===Syntax===
{{sdcl list begin}}
+
{{sdsc begin}}
{{sdcl list item | {{ttb|nullptr}}| notes={{mark since c++11}}}}
+
{{sdsc|{{ttb|nullptr}}}}
{{sdcl list end}}
+
{{sdsc end}}
  
 
===Explanation===
 
===Explanation===
The keyword {{tt|nullptr}} denotes the null pointer literal. It is an unspecified prvalue of type {{c|std::nullptr_t}}. There exist {{rlp|implicit_cast|implicit conversions}} from {{tt|nullptr}} to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any value of type {{c|std::nullptr_t}} as well as for the macro {{c|NULL}}, the null pointer constant.
+
The keyword {{tt|nullptr}} denotes the pointer literal. It is a {{rlp|value category|prvalue}} of type {{lc|std::nullptr_t}}. There exist {{rlp|implicit conversion}}s from {{tt|nullptr}} to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of type {{lc|std::nullptr_t}} as well as the macro {{lc|NULL}}.
 +
 
 +
===Keywords===
 +
{{ltt|cpp/keyword/nullptr}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| Demonstrates how nullptr allows forwarding via a template function.
+
|Demonstrates that {{tt|nullptr}} retains the meaning of null pointer constant even if it is no longer a literal.
| code=
+
|code=
 
#include <cstddef>
 
#include <cstddef>
 
#include <iostream>
 
#include <iostream>
  
template<class F, class A>
+
template<class T>
void Fwd(F f, A a)
+
constexpr T clone(const T& t)
 
{
 
{
     f(a);
+
     return t;
 
}
 
}
  
void g(int* i)
+
void g(int*)
 
{
 
{
 
     std::cout << "Function g called\n";
 
     std::cout << "Function g called\n";
Line 30: Line 33:
 
int main()
 
int main()
 
{
 
{
 +
    g(nullptr);        // Fine
 
     g(NULL);          // Fine
 
     g(NULL);          // Fine
 
     g(0);              // Fine
 
     g(0);              // Fine
  
     Fwd(g, nullptr);   // Fine
+
     g(clone(nullptr)); // Fine
//  Fwd(g, NULL); // ERROR: No function g(int)
+
//  g(clone(NULL));   // ERROR: non-literal zero cannot be a null pointer constant
 +
//  g(clone(0));      // ERROR: non-literal zero cannot be a null pointer constant
 
}
 
}
| output=
+
|output=
 +
Function g called
 
Function g called
 
Function g called
 
Function g called
 
Function g called
Line 42: Line 48:
 
}}
 
}}
  
===Keywords===
+
===References===
{{ltt|cpp/keywords/nullptr}}
+
{{ref std c++23}}
 +
{{ref std|section=7.3.12|title=Pointer conversions|id=conv.ptr}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=7.3.12|title=Pointer conversions|id=conv.ptr}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section=7.11|title=Pointer conversions|id=conv.ptr}}
 +
{{ref std end}}
 +
{{ref std c++14}}
 +
{{ref std|section=4.10|title=Pointer conversions|id=conv.ptr}}
 +
{{ref std end}}
 +
{{ref std c++11}}
 +
{{ref std|section=4.10|title=Pointer conversions|id=conv.ptr}}
 +
{{ref std end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/types/dcl list NULL}}
+
{{dsc inc|cpp/types/dsc NULL}}
{{dcl list template | cpp/types/dcl list nullptr_t}}
+
{{dsc inc|cpp/types/dsc nullptr_t}}
{{dcl list end}}
+
{{dsc see c|c/language/nullptr}}
 +
{{dsc end}}
  
[[zh:cpp/language/nullptr]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 15:58, 12 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Contents

[edit] Syntax

nullptr

[edit] Explanation

The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL.

[edit] Keywords

nullptr

[edit] Example

Demonstrates that nullptr retains the meaning of null pointer constant even if it is no longer a literal.

#include <cstddef>
#include <iostream>
 
template<class T>
constexpr T clone(const T& t)
{
    return t;
}
 
void g(int*)
{
    std::cout << "Function g called\n";
}
 
int main()
{
    g(nullptr);        // Fine
    g(NULL);           // Fine
    g(0);              // Fine
 
    g(clone(nullptr)); // Fine
//  g(clone(NULL));    // ERROR: non-literal zero cannot be a null pointer constant
//  g(clone(0));       // ERROR: non-literal zero cannot be a null pointer constant
}

Output:

Function g called
Function g called
Function g called
Function g called

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 7.3.12 Pointer conversions [conv.ptr]
  • C++20 standard (ISO/IEC 14882:2020):
  • 7.3.12 Pointer conversions [conv.ptr]
  • C++17 standard (ISO/IEC 14882:2017):
  • 7.11 Pointer conversions [conv.ptr]
  • C++14 standard (ISO/IEC 14882:2014):
  • 4.10 Pointer conversions [conv.ptr]
  • C++11 standard (ISO/IEC 14882:2011):
  • 4.10 Pointer conversions [conv.ptr]

[edit] See also

implementation-defined null pointer constant
(macro constant) [edit]
(C++11)
the type of the null pointer literal nullptr
(typedef) [edit]
C documentation for nullptr