user1773602
user1773602

Reputation:

A 'typedef' for a template function (boost::make_shared)

I'm migrating my project to C++11 and I'm trying to use as much of standard library as possible.

Before I finalise the migration, I need a quick way to flip between boost and STL implementation of shared_ptr (to do benchmarks, unit tests etc).

So I defined an alias for shared_ptr like this:

#ifdef _USE_BOOST_
template <class C>
using shared_ptr = boost::shared_ptr<C>
#else
template <class C>
using shared_ptr = std::shared_ptr<C>
#endif 

now I need to do the same for make_shared... But how? Macro? A wrapper? I don't really like either of them. What are the alternatives?

Upvotes: 6

Views: 808

Answers (1)

ecatmur
ecatmur

Reputation: 157374

Using variadic templates and perfect forwarding:

template<typename C, typename...Args>
shared_ptr<C> make_shared(Args &&...args) {
#ifdef _USE_BOOST_
  return boost::make_shared<C>(std::forward<Args>(args)...);
#else
  return std::make_shared<C>(std::forward<Args>(args)...);
#endif
}

Upvotes: 7

Related Questions