Skip to content

Latest commit

 

History

History

item-8-perfer-nullptr-to-0-and-null

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Item 8. Prefer nullptr to 0 and NULL

Test Codes

Notes

Type of nullptr

  • It doesn't have a pointer type, but you can think of it as a pointer of all types.
  • nullptr's actual type is std::nullptr_t, which defined as typedef decltype(nullptr) nullptr_t;(see std::nullptr_t).
  • The type std::nullptr_t implicitly converts to all raw pointer types, and that's what makes nullptr act as if it were a pointer of all types.

Advantages

  • Using nullptr instead of 0 or NULL thus avoids overload resolution surprises.
    // three overloads of `f`
    void f(int);
    void f(bool);
    void f(void*);

    f(0);    // calls f(int)
    f(NULL); // might not compile, but typically calls f(int). Never calls f(void*)

    f(nullptr); // calls f(void*), i.e. only nullptr works
  • Improves code clarity, espically when auto variables are involved.
    auto result = findRecord( /* arguments */ );
    
    // type of result may not be clear
    if (result == 0) {      // can not know result is integer or pointer
        // ... 
    }

    // no ambiguity
    if (result == nullptr){ // result must be a pointer
        // ...
    }
    void f(void*){
        std::cout << "function f" << std::endl;
    }

    template<typename FuncType, typename PtrType>
    decltype(auto) call(FuncType func, PtrType ptr) {
        return func(ptr);
    }

    // both compile error! 
    // because both 0 and NULL will be deduced to integer(`int` or `long int`) 
    call(f, 0); 
    call(f, NULL);

    // works! 
    call(f, nullptr); // output "function f"

References