Q:

Virtual constructor with shared_ptr

I am interested in whether it is possible to replicate the behaviour of the virtual constructor pattern (e.g. see virtual constructor example) using std::shared_ptr. The approach of replacing the raw pointers by shared pointers fails for obvious reasons (invalid covariant return type). I am interested if anyone knows of any alternatives that would support smart pointers.
Smart pointers are used everywhere in the project and the virtual constructor-like approach seems to be the only approach possible for the problem I am currently working on.
The code is provided below:
class A
{

public:

    virtual std::shared_ptr<A> clone(void) const = 0;

    virtual void mymethod() const = 0;

};

class B : public A
{

    std::shared_ptr<B> clone(void) const
        {
            return (new B(*this));
        }

    void mymethod() const
        {
            std::cout << "type something";
        }

};

class C
{

public:

    void mymethod(std::shared_ptr<A const> MyB)
        {
            std::shared_ptr<A const> MyB2 = MyB -> clone();
            MyB2 -> mymethod();
        }

};

A:

C++'s covariant return type feature only supports raw pointers and references.
Supporting a single instance of clone function override is simple, but centralizing the boilerplate for multiple instances of clone function overriding is more problematic. Essentially this is the problem of expressing a covariant function implementation in a generic way, which C++ lacks direct support for. Possible solutions to that code generation include

A macro.
With C++03 this was the clear winner, but with C++11 and later it's only marginally (and possibly subjectively) the best.
A middle-man inheritance mixin.
This was technically possibly in C++03 but forwarding of constructor arguments became extremely much simpler in C++11.
Dominance in virtual inheritance hierarchy.
A complex technique that relies on a not well-known language feature somewhere.

Since the dominance solution is very complex and is only of academic interest, below I only exemplify (1) manual implementation of a single clone override, (2) defining a suitable macro, and (3) middle-man inheritance mixin.

Example of manual implementation of a single instance of clone function override:
// Manual implementation of a single clone function override.

#include <memory>

namespace my {
    using std::shared_ptr;

    class A
    {
    private:
        virtual auto virtual_clone() const
            -> A*
        { return new A( *this ); }

    public:
        auto clone() const
            -> shared_ptr<A>
        { return shared_ptr<A>( virtual_clone() ); }

        virtual void m() const {}
    };

    class B
        : public A
    {
    private:
        auto virtual_clone() const
            -> B* override
        { return new B( *this ); }

    public:
        auto clone() const
            -> shared_ptr<B> 
        { return shared_ptr<B>( virtual_clone() ); }
    };
}  // namespace my

void foo( std::shared_ptr<my::A const> b )
{
    std::shared_ptr<my::A const> b2 = b->clone();
    b2->m();
}

auto main()
    -> int
{ foo( std::shared_ptr<my::A>( new my::B ) ); }

Example of macro as a general solution:
// Macro-based centralization of covariant boilerplate code.

#include <memory>

#define MY_CLONE_IMPL( classname )          \
    private:                                \
        virtual auto virtual_clone() const  \
            -> classname*                   \
        { return new classname( *this ); }  \
    public:                                 \
        auto clone() const                  \
            -> std::shared_ptr<classname>   \
        { return std::shared_ptr<classname>( virtual_clone() ); }

void say( char const* );

namespace my {
    using std::shared_ptr;

    class A
    {
        MY_CLONE_IMPL( A )
    public:
        virtual void m() const { say( "A::m" ); }
    };

    class B
        : public A
    {
        MY_CLONE_IMPL( B )
    public:
        virtual void m() const { say( "B::m" ); }
    };
}  // namespace my

void foo( std::shared_ptr<my::A const> b )
{
    std::shared_ptr<my::A const> b2 = b->clone();
    b2->m();
}

#include <iostream>
void say( char const* s ) { std::cout << s << "\n"; }

auto main()
    -> int
{ foo( std::shared_ptr<my::A>( new my::B ) ); }

Example of a middle-man inheritance mixin as general solution:
// Middle-man mixin centralization of covariant boilerplate code.

#include <memory>       // std::shared_ptr
#include <utility>      // std::forward

struct Void {};

template< class Derived, class Base >
class With_cloning_of_
    : public Base
{
private:                              
    virtual auto virtual_clone() const
        -> Base*
    { return new Derived( static_cast<Derived const&>( *this ) ); }

public:                               
    auto clone() const                
        -> std::shared_ptr<Derived> 
    {
        return std::shared_ptr<Derived>(
            static_cast<Derived*>( virtual_clone() )
            );
    }

    template< class... Args >
    With_cloning_of_( Args&&... args )
        : Base( std::forward<Args>( args )... )
    {}
};

void say( char const* );

namespace my {
    using std::shared_ptr;

    class A
        : public With_cloning_of_<A, Void>
    {
    public:
        virtual void m() const { say( "A::m" ); }
    };

    class B
        : public With_cloning_of_<B, A>
    {
    public:
        virtual void m() const { say( "B::m" ); }
    };
}  // namespace my

void foo( std::shared_ptr<my::A const> b )
{
    std::shared_ptr<my::A const> b2 = b->clone();
    b2->m();
}

#include <iostream>
void say( char const* s ) { std::cout << s << "\n"; }

auto main()
    -> int
{ foo( std::shared_ptr<my::A>( new my::B ) ); }

If you want to use std::make_shared for the cloning then this complicates things a little.
One way to go about it, that doesn't introduce additional state like shared_from_this does, is to let the virtual cloning function return a shared_ptr to a known common ancestor.
One main problem with that is that introduces a degree of freedom, freedom to introduce bugs, but still that's just what one often does in C++, so I give this example:
// std::make_shared with middle-man mixin.

#include <memory>       // std::shared_ptr
#include <utility>      // std::forward

struct Void {};

template< class Derived, class Base, class Common_ancestor >
class With_cloning_of_
    : public Base
{
private:
    virtual auto virtual_clone() const
        -> std::shared_ptr<Common_ancestor>
    { return std::make_shared<Derived>( static_cast<Derived const&>( *this ) ); }

public:                               
    auto clone() const                
        -> std::shared_ptr<Derived> 
    { return std::static_pointer_cast<Derived>( virtual_clone() ); }

    template< class... Args >
    With_cloning_of_( Args&&... args )
        : Base( std::forward<Args>( args )... )
    {}
};

void say( char const* );

namespace my {
    using std::shared_ptr;

    class A
        : public With_cloning_of_<A, Void, Void>
    {
    public:
        virtual void m() const { say( "A::m" ); }
    };

    class B
        : public With_cloning_of_<B, A, Void>
    {
    public:
        virtual void m() const { say( "B::m" ); }
    };
}  // namespace my

void foo( std::shared_ptr<my::A const> b )
{
    std::shared_ptr<my::A const> b2 = b->clone();
    b2->m();
}

#include <iostream>
void say( char const* s ) { std::cout << s << "\n"; }

auto main()
    -> int
{ foo( std::shared_ptr<my::A>( new my::B ) ); }

