What are the uses of the type `std::nullptr_t`?

前端 未结 3 1196
再見小時候
再見小時候 2021-01-01 13:38

I learned that nullptr, in addition to being convertible to any pointer type (but not to any integral type) also has its own type std::nullptr_t. S

3条回答
  •  天命终不由人
    2021-01-01 13:55

    If more than one overload accepts a pointer type, an overload for std::nullptr_t is necessary to accept a nullptr argument. Without the std::nullptr_t overload, it would be ambiguous which pointer overload should be selected when passed nullptr.

    Example:

    void f(int *intp)
    {
        // Passed an int pointer
    }
    
    void f(char *charp)
    {
        // Passed a char pointer
    }
    
    void f(std::nullptr_t nullp)
    {
        // Passed a null pointer
    }
    

提交回复
热议问题