Namespaces
Variants

nullptr , the pointer literal (since C++11)

From cppreference.net
C++ language
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
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
Special member functions
Templates
Miscellaneous

目次

構文

nullptr

説明

キーワード nullptr はポインタリテラルを表します。これは prvalue であり、型は std::nullptr_t です。 nullptr からあらゆるポインタ型およびあらゆるメンバポインタ型のヌルポインタ値への 暗黙変換 が存在します。同様の変換は、あらゆるヌルポインタ定数に対しても存在し、これには std::nullptr_t 型の値だけでなく、マクロ NULL も含まれます。

キーワード

nullptr

nullptr がリテラルでなくなった後も、ヌルポインタ定数の意味を保持することを示す。

#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);        // 正常
    g(NULL);           // 正常
    g(0);              // 正常
    g(clone(nullptr)); // 正常
//  g(clone(NULL));    // エラー: 非リテラルのゼロはヌルポインタ定数になれない
//  g(clone(0));       // エラー: 非リテラルのゼロはヌルポインタ定数になれない
}

出力:

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

参考文献

  • C++23標準 (ISO/IEC 14882:2024):
  • 7.3.12 ポインタ変換 [conv.ptr]
  • C++20 標準 (ISO/IEC 14882:2020):
  • 7.3.12 ポインタ変換 [conv.ptr]
  • C++17 標準 (ISO/IEC 14882:2017):
  • 7.11 ポインタ変換 [conv.ptr]
  • C++14標準 (ISO/IEC 14882:2014):
  • 4.10 ポインタ変換 [conv.ptr]
  • C++11標準 (ISO/IEC 14882:2011):
  • 4.10 ポインタ変換 [conv.ptr]

関連項目

実装定義のヌルポインタ定数
(マクロ定数)
(C++11)
ヌルポインタリテラル nullptr の型
(typedef)