[ create a new paste ] login | about

Link: http://codepad.org/AileOQEQ    [ raw code | output | fork ]

C++, pasted on May 1:
#include <memory>
#include <utility>
using namespace std;

template< class Type, class Deleter = std::default_delete<Type> >
class make_unique
    : public unique_ptr< Type, Deleter >
{
public:
    template< class... Args >
    make_unique( Args&&... args )
        : unique_ptr< Type, Deleter >(
            new Type( forward<Args>( args )... ), Deleter()
            )
    {}
};

class Blah
    : public enable_shared_from_this< Blah >
{
template< class Type> friend struct std::default_delete;
template< class Type, class Deleter > friend class make_unique;

private:
    Blah& operator=( Blah const& ); // = delete

protected:
    Blah( Blah const& other )
        : enable_shared_from_this( other )
    {}          // = default

    virtual ~Blah(){}               // Ensure dynamic alloc only.

    static void* operator new( size_t const size )
    { return ::operator new( size ); }

public:
    Blah() {}
    Blah( int ) {}
    Blah( char const* ) {}
};

int main()
{
    //Blah                o;                              // !Nah.
    //shared_ptr<Blah>    p( new Blah( "blah" ) );        // !Nah.

    shared_ptr<Blah>    p   = make_unique<Blah>( "blah" );
    shared_ptr<Blah>    q   = p->shared_from_this();
}


Output:
1
2
Line 5: error: expected type-specifier
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: