While I have std::tr1::shared_ptr
available in my compiler, I don\'t
have make_shared
.
Can someone point me to a proper implementa
If your compiler don't give an implementation of make_shared and you can't use boost, and you don't mind the lack of single-allocation optimization both for the object and the reference counter then make_shared is something like this:
Without variadic template support:
// zero arguments version
template
inline shared_ptr make_shared()
{
return shared_ptr(new T());
}
// one argument version
template
inline shared_ptr make_shared(Arg1&& arg1)
{
return shared_ptr(new T(std::forward(arg1)));
}
// two arguments version
template
inline shared_ptr make_shared(Arg1&& arg1, Arg2&& arg2)
{
return shared_ptr(new T(std::forward(arg1),
std::forward(arg2)));
}
// ...
If your compiler don't support r-value references, then make 2 versions for each arguments count: one const Arg& and one Arg&
With variadic template support:
template
inline shared_ptr make_shared(Args&&... args)
{
return shared_ptr(new T( std::forward(args)... ));
}