Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
511 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...