types_nullptr_t

types_nullptr_t (nullptr_t 타입)

std::nullptr_t는 null 포인터 리터럴인 nullptr의 타입이에요. 이 타입은 그 자체로는 포인터 타입도 아니고 멤버 포인터 타입도 아닌 별개의 타입이에요. 이 타입의 prvalue는 null 포인터 상수로 취급되며, 모든 포인터 타입과 멤버 포인터 타입으로 암시적으로 변환될 수 있어요.

출처: cppreference

본문

<cstddef> 헤더에 정의됨
using nullptr_t = decltype(nullptr); (C++11부터)

std::nullptr_t는 null 포인터 리터럴 nullptr의 타입이에요. 이 타입은 그 자체로는 포인터 타입도 아니고 멤버 포인터 타입도 아닌 별개의 타입이에요. 이 타입의 prvalue는 null 포인터 상수이며, 모든 포인터 타입과 멤버 포인터 타입으로 암시적으로 변환될 수 있어요.

sizeof(std::nullptr_t)sizeof(void*)와 같아요.

Notes

C++ 표준은 <stddef.h><cstddef>의 내용을 전역 네임스페이스에 배치하도록 요구해요. 따라서 <stddef.h>를 포함하면 nullptr_t가 전역 네임스페이스에서 사용 가능해야 해요.

nullptr_t는 C23 이전의 C에는 포함되지 않아요.

std::nullptr_t의 선언이 다른 표준 라이브러리 헤더에서 사용 가능한지 여부는 명시되어 있지 않아요. 구현은 표준이 std::nullptr_t를 사용하도록 요구하는 경우에도 decltype(nullptr)처럼 표현하는 방식으로 이 이름을 도입하지 않을 수 있어요.

Example

두 개 이상의 오버로드가 서로 다른 포인터 타입을 받는다면, null 포인터 인자를 받기 위해 std::nullptr_t용 오버로드가 필요해요.

#include <cstddef>
#include <iostream>

void f(int*)
{
    std::cout << "Pointer to integer overload\n";
}

void f(double*)
{
    std::cout << "Pointer to double overload\n";
}

void f(std::nullptr_t)
{
    std::cout << "null pointer overload\n";
}

int main()
{
    int* pi{};
    double* pd{};
    
    f(pi);
    f(pd);
    f(nullptr); // would be ambiguous without void f(nullptr_t)
    // f(0);    // ambiguous call: all three functions are candidates
    // f(NULL); // ambiguous if NULL is an integral null pointer constant 
                // (as is the case in most implementations)
}

출력:

Pointer to integer overload
Pointer to double overload
null pointer overload

See also

nullptr (C++11) null 포인터 값을 지정하는 포인터 리터럴 [edit]
NULL 구현 정의 null 포인터 상수 (매크로 상수) [edit]
is_null_pointer (C++11) (DR*) 타입이 std::nullptr_t인지 확인하는 클래스 템플릿 [edit]
C 문서의 nullptr_t

더 알아보기 (Learn more)

cppreference