是否可以创建 std::make_shared
的 boost phoenix 惰性变体?我的意思是,让类似的事情成为可能
namespace p = boost::phoenix;
...
expr = custom_parser[_a=p::make_shared<Node>(_1,_2,_3)] >> ...
由于 std::make_shared
的可变模板性质,不能使用 BOOST_PHOENIX_ADAPT_FUNCTION
。所以,如果可能的话,包装器应该是可变参数模板本身。
最佳答案
如果你能省下一组额外的括号:
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> >;
}
你可以像这样使用
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 << " ";
关于c++ - 如何创建boost phoenix make_shared?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21516201/