std::nullptr_t
| 헤더 <cstddef>
|
||
using nullptr_t = decltype(nullptr);
|
(C++11부터)에 정의됨 | |
std::nullptr_t는 널 포인터 리터럴 nullptr의 타입입니다. 이는 그 자체로 포인터 타입이나 멤버 포인터 타입이 아닌 별개의 타입입니다. 이 타입의 prvalue는 널 포인터 상수이며, 모든 포인터 및 멤버 포인터 타입으로 암시적 변환될 수 있습니다.
sizeof(std::nullptr_t)는 sizeof(void *)와 같습니다.
참고 사항
C++ 표준은
<stddef.h>
가
<cstddef>
의 내용을 전역 네임스페이스에 배치하도록 요구하며, 이에 따라
<stddef.h>
가 포함될 때
nullptr_t
가 전역 네임스페이스에서 사용 가능해야 합니다.
nullptr_t
는 C23 이전까지 C 언어의 일부가 아닙니다.
std::nullptr_t
의 선언이 다른 표준 라이브러리 헤더에서 사용 가능한지 여부는 규정되어 있지 않습니다. 구현체는 표준에서
std::nullptr_t
사용을 요구하는 경우에도, 예를 들어 대신
decltype
(
nullptr
)
과 같은 방식으로 기술함으로써 이 이름의 도입을 피할 수 있습니다.
예제
두 개 이상의 오버로드가 서로 다른 포인터 타입을 받는 경우, 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
참고 항목
| nullptr (C++11) | 널 포인터 값을 지정하는 포인터 리터럴 |
|
구현 정의 널 포인터 상수
(매크로 상수) |
|
|
(C++11)
(
DR*
)
|
타입이
std::nullptr_t
인지 확인
(클래스 템플릿) |
|
C 문서
for
nullptr_t
|
|