how to create boost phoenix make_shared?

boost, boost-phoenix, c++, make-shared

Solution

If you can spare an extra set of parentheses:

namespace {
    template <typename T>
    struct make_shared_f
    {
        template <typename... A> struct result 
            { typedef boost::shared_ptr<T> type; };

        template <typename... A>
        typename result<A...>::type operator()(A&&... a) const {
            return boost::make_shared<T>(std::forward<A>(a)...);
        }
    };

    template <typename T>
    using make_shared_ = boost::phoenix::function<make_shared_f<T> >;
}

Which you can uses like

typedef std::vector<int> IntVec;
auto LazyInts = make_shared_<IntVec>()(arg1, arg2);

// create a shared vector of 7 ints '42'
auto ints = LazyInts(7, 42);
for (auto i : *ints) std::cout << i << " ";

See it Live on Coliru

Problem

Is it possible to create boost phoenix lazy variant of `std::make_shared`? I mean, to make possible something like ``` namespace p = boost::phoenix; ... expr = custom_parser[_a=p::make_shared<Node>(_1,_2,_3)] >> ... ``` One cannot use `BOOST_PHOENIX_ADAPT_FUNCTION` because of variadic template nature of `std::make_shared`. So, probably wrapper should be variadic template itself, if it is possible to write one.

Original source