nullptr
, the pointer literal
(since C++11)
| General topics | ||||||||||||||||
| Flow control | ||||||||||||||||
| Conditional execution statements | ||||||||||||||||
| Iteration statements (loops) | ||||||||||||||||
|
||||||||||||||||
| Jump statements | ||||||||||||||||
| Functions | ||||||||||||||||
| Function declaration | ||||||||||||||||
| Lambda function expression | ||||||||||||||||
inline
specifier
|
||||||||||||||||
| Dynamic exception specifications ( until C++17* ) | ||||||||||||||||
noexcept
specifier
(C++11)
|
||||||||||||||||
| Exceptions | ||||||||||||||||
| Namespaces | ||||||||||||||||
| Types | ||||||||||||||||
| Specifiers | ||||||||||||||||
|
||||||||||||||||
| 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 | ||||||||||||||||
|
||||||||||||||||
| Special member functions | ||||||||||||||||
|
||||||||||||||||
| Templates | ||||||||||||||||
| Miscellaneous | ||||||||||||||||
| General | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Literals | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Operators | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
| Conversions | |||||||||||||||||||||||||||||||||||||||||||||||||||
Inhaltsverzeichnis |
Syntax
nullptr
|
|||||||||
Erklärung
Das Schlüsselwort
nullptr
bezeichnet den Pointer-Literal. Es ist ein
Prvalue
vom Typ
std::nullptr_t
. Es existieren
implizite Konvertierungen
von
nullptr
zum Nullzeigerwert jedes Pointer-Typs und jedes Pointer-zu-Mitglied-Typs. Ähnliche Konvertierungen existieren für jede Nullzeiger-Konstante, die Werte vom Typ
std::nullptr_t
sowie das Makro
NULL
einschließt.
Schlüsselwörter
Beispiel
Zeigt, dass
nullptr
die Bedeutung einer Null-Zeiger-Konstante beibehält, selbst wenn es kein Literal mehr ist.
#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 }
Ausgabe:
Function g called Function g called Function g called Function g called
Referenzen
- C++23-Standard (ISO/IEC 14882:2024):
-
- 7.3.12 Zeigerkonvertierungen [conv.ptr]
- C++20-Standard (ISO/IEC 14882:2020):
-
- 7.3.12 Zeigerkonvertierungen [conv.ptr]
- C++17 Standard (ISO/IEC 14882:2017):
-
- 7.11 Pointer-Konvertierungen [conv.ptr]
- C++14-Standard (ISO/IEC 14882:2014):
-
- 4.10 Zeiger-Konvertierungen [conv.ptr]
- C++11-Standard (ISO/IEC 14882:2011):
-
- 4.10 Zeiger-Konvertierungen [conv.ptr]
Siehe auch
|
implementierungsdefinierte Nullzeigerkonstante
(Makrokonstante) |
|
|
(C++11)
|
der Typ des Nullzeigerliterals
nullptr
(Typedef) |
|
C-Dokumentation
für
nullptr
|
|