Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Tags
more
Archives
Today
Total
관리 메뉴

Visual Studio

nullptr (cppreference 번역 및 정리) 본문

Programming Language

nullptr (cppreference 번역 및 정리)

emacser 2022. 8. 29. 04:24

nullptr 키워드는 std::nullptr_t 타입의 prvalue 리터럴이다. nullptr는 어떤 포인터 타입이나 멤버 포인터 타입의 null 포인터와도 암시적인 형변환이 가능하며, 매크로 NULL과 같이 타입 std::nullptr_t에 포함되는 값을 가진 어떤 null 포인터 상수와도 암시적 형변환이 가능하다.

std::nullptr_t

std::nullptr_t는 그 자체로는 포인터 타입이 아닌 별개의 타입이며, null포인터 리터럴인 nullptr의 타입이다. 어떤 포인터 타입이나 멤버 포인터 타입과도 암시적 형변환이 가능하다.

sizeof(std::nullptr_t) 는 *sizeof(void ) 과 동일하다.

예제

nullptr가 더이상 리터럴이 아니게 되어도 null 포인터 상수의 의미를 유지함을 보여주는 예제이다.

소스코드

#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

 

'Programming Language' 카테고리의 다른 글

Variable Shadowing  (0) 2022.07.11
lvalue + xvalue = glvalue  (0) 2022.07.08
유니코드 한글 초성 중성 종성 분리하기  (0) 2022.06.26
memset의 함정 (은근 착각하기 쉬움)  (0) 2022.06.19
가상함수, RTTI  (0) 2022.06.14