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);        // 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
}

출력:

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)
C documentation for nullptr