C++/Syntax

C++ nullptr_t

Caniro 2021. 3. 11. 16:52

nullptr_t

  • C++11

  • C언어에서 사용되지는 않으나, <cstddef> 라이브러리에 존재하는 자료형이다.

    #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)
    }

--- 참고