const // It is a const object...
class nullptr_t
{
public:
template<class T>
inline operator T*() const // convertible to any type of null non-member pointer...
{ return 0; }
template<class C, class T>
inline operator T C::*() const // or any type of null member pointer...
{ return 0; }
private:
void operator&() const; // Can't take address of nullptr
} nullptr = {};
operator T*() const
and operator T C::*() const
are already defined in class, so it can inline automatically. So, why add inline
again? void operator&() const;
, notvoid operator&() = delete
? nullptr = {};
mean? Nullptr vs NULL NULL is 0 (zero) i.e. integer constant zero with C-style typecast to void* , while nullptr is prvalue of type nullptr_t , which is an integer literal that evaluates to zero. For those of you who believe that NULL is the same i.e. (void*)0 in C and C++.
Also, remembering that nullptr evaluates to false is another bit of information you need to store in the back of your brain to properly decode the code you're reading.
The nullptr keyword can be used to test if a pointer or handle reference is null before the reference is used. Function calls among languages that use null pointer values for error checking should be interpreted correctly. You cannot initialize a handle to zero; only nullptr can be used.
The Effective C++ is still very relevant, and is not superseded by the new book.
Adding the extra inline is a matter of style. That specifier has to do with linkage and ODR violations, not actual inlining. All inline member function definitions and template member functions in general have that specifier implicitly. I assume Scott Meyers added it there for pedagogical reasons.
Effective C++ was written for C++03 originally. There was no = delete
back then. Declaring but not defining a function was all you could do back then.
This is aggregate initialization. This implementation of nullptr_t
can be initialized like that even in C++03. It creates the value of nullptr
. Since nullptr_t
doesn't have a user provided default c'tor, it's required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With