Defer pattern for constructors in C++
Identifiers with leading underscore(s) followed by a capital letter are reserved for the implementation and should not be used in your own code. It's easiest to just avoid leading underscores.
When using enable_shared_from_this it's important to use a static factory function and make the other constructors private. This prevents calling the Example constructor directly (where calling shared_from_this will be undefined behaviour, since we don't have a shared_ptr).




Do we really need enable_shared_from_this? The whole thing could be more easily written as:

    class Example
    {
    private:

        Example() { }

    public:

        static std::shared_ptr&lt;Example&gt; Create()
        {
            auto example = std::make_shared&lt;Example&gt;();
            example-&gt;do_the_thing();

            return example;
        }
    };


It really depends on what do_the_thing() is...

If we have to register the class somewhere after creation (or consistently perform some action), perhaps that logic doesn't belong in the Example class at all.

struct Example {};

struct Registry
{
    template&lt;class T, class... Args&gt;
    std::shared_ptr&lt;T&gt; CreateAndRegister(Args&amp;&amp;... args)
    {
        auto object = std::make_shared&lt;T&gt;(std::forward&lt;Args&gt;(args)...);
        Register(object);
        return object;
    }
};

auto registry = Registry();
auto e = registry.CreateAndRegister&lt;Example&gt;();


While an object has control over it's own copy / move semantics, it generally shouldn't care who / what owns it.

So the question is: why does Example need to "break the 4th wall" and know that it exists inside a shared_ptr?



Large inheritance hierarchies are out of fashion nowadays (with good reason), but with a general solution, you may need to cope with:

struct ExampleA : enable_defer, enable_shared_from_this&lt;ExampleA&gt; {};
struct ExampleB : ExampleA {}; // what if B wants to defer something?


and

struct ExampleA : enable_defer, enable_shared_from_this&lt;ExampleA&gt; {};
struct ExampleB : enable_defer, enable_shared_from_this&lt;ExampleB&gt; {};
struct ExampleC : ExampleA, ExampleB {}; // uh oh... ?


shared_from_this is quite complicated to use as it is with a class hierarchy.