Implementation of nullptr in c++98
17 January 2023
Introduced in c++11, nullptr
is a useful keyword, the advantage of nullptr
over NULL
is, that nullptr
acts like a real pointer type thus it adds type
safety whereas NULL
acts like an integer just set to 0
in pre c++11.
Contents
Implementation
const class nullptr_t { // is a const object
public:
// convertible to any type of null non-member pointer...
template<class T>
operator T*() const {
return 0;
}
// or any type of null member pointer...
template<class C, class T>
operator T C::*() const {
return 0;
}
private:
// cant take address of nullptr
void operator&() const;
} nullptr = {}; // and whose name is nullptr