Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "inline operator T*() const" mean?

Tags:

c++

I reviewed some codes on nullptr and found that:

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 = {};

what do inline operator T*() const and inline operator T C::*() const mean?

Do they work the same way as inline T operator *() const or inline T operator C::*() const?

Why not specify the return type in the declaration?


1 Answers

They are user defined conversion operator functions.

A simpler function:

struct Foo
{
   inline operator int () const { return 0; }
};

Given that you can use

Foo f;
int i = f; // Invokes f.operator int () and initializes i with
           // return value.

For you class, which is an anonymous class for nullptr, what it means is that it can be converted to any pointer or member function pointer. You can use:

int* ip = nullptr;

It uses the return value of the first user defined conversion operator function to initialize ip.

struct Bar
{
   void bar() {}
};

void (Bar::*ptr)() = nullptr;

It uses the return value of the second user defined conversion operator function to initialize ptr.

like image 198
R Sahu Avatar answered Jul 31 '26 20:07

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!