LaoJiu
LaoJiu

Reputation: 69

Custom "nullptr", but how to understand the codes?

It's from Wikipedia, if our compiler don't support c++11 , we can implement one by ourselves , just like below:

const class nullptr_t
{
public:
    template<class T>
    inline operator T*() const
        { return 0; }

    template<class C, class T>
    inline operator T C::*() const
        { return 0; }

private:
    void operator&() const;
} nullptr = {};

I can't understand the above codes.

----------------------update------------------

Sorry , i didn't express it clear.

 template<class T>
    inline operator T*() const
        { return 0; }

 template<class C, class T>
    inline operator T C::*() const
        { return 0; }

The above codes,i don't understand the grammar. I never see this kind of template form before.(like "operator T C::*()")

Upvotes: 0

Views: 257

Answers (1)

Peter
Peter

Reputation: 36597

template<class T>
inline operator T*() const
    { return 0; }

means that an object of type nullptr_t can be implicitly converted to any (non-member) pointer type. It is simply an operator T *() function (conversion to type T *) templated for any type T.

template<class C, class T>
inline operator T C::*() const
    { return 0; }

means that an object of type nullptr_t can be converted to a pointer to a non-static member of any class (template parameter C) of any type (template parameter T).

Both operators are const, so cannot change the nullptr_t object.

Upvotes: 4

Related Questions