mercredi 2 mai 2018

shared_from_this() of an instance that is not yet owned by shared_ptr always returns null?

I am trying to create a member function that returns whether it has stored to shared_ptr.

class X : public std::enable_shared_from_this {
    ...
    bool is_shared() const {
        return shared_from_this();
    }
};

...

(new X())->is_shared(); // -> false?

Is this legal? In this case, is shared_from_this() guaranteed to return null and not throw any exception?

Aucun commentaire:

Enregistrer un commentaire