ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C++] nullptr vs NULL
    프로그래밍 언어 2018. 12. 14. 09:03
    C언어에서 NULL은 (void*)0
    C++ 11 이전까지 NULL은 0으로 정의되어있었다.

    C++ 진영에서 null pointer와 숫자 0이 구분이 불명확한 문제가 제기되었다.

    이 문제점이 개선된 C++11에서는
    nullptr은 포인터만을 의미한다.

    다음 예제를 참고하자:
    // Overloading case
    void f(char*)
    void f(int)

    f (nullptr) // calls f(char*)
    f (0)        // calls f (int)

    // deduction to nullptr_t, no deduction to pointer type.
    template<typename T> void g (T* t)

    g (nullptr);        // error
    g ( (float*) nullptr );  // Deduction T = float

    template<typename T> void h (T t)

    h (0);        // deduce T = int
    h (nullptr); // deduces T = nullptr_t
    h ( (float*) nullptr );  // Deduction T = float*
Designed by Tistory.