[英]C++ std::function that returns a vector of it's own type (recursive types again)
理想情况下,我想声明以下类型:
using action_t = std::function< std::vector< action_t >(void) >
这是一个重击,返回了后续重击的向量。 通过使用递归typedef函数定义中的信息,我能够做到这一点:std :: function返回自己的类型 :
struct RecursiveHelper
{
typedef std::vector<RecursiveHelper> rtype;
typedef std::function< rtype (void) > ftype;
RecursiveHelper( ftype f ) : func(f) {}
rtype operator()() const { return func(); }
operator ftype () { return func; }
ftype func;
};
using action_t = RecursiveHelper;
using actions_t = std::vector<RecursiveHelper>;
但是,例如,要将这些东西压入堆栈,我必须做这样的事情:
std::stack<action_t> stack;
stack.push(RecursiveHelper([&visitor, &node](void){
return visitor.visitNode(node);
}));
理想情况下,我想避免在使用这种东西的代码中提及RecursiveHelper
,如果他们想要一堆action_t,他们应该能够将符合条件的lambda直接推入其中。
有什么办法可以做到这一点?
编写一个接受任何可转换为ftype
并且不是RecursiveHelper
函数对象的构造函数:
template<class F, class = std::enable_if_t<std::is_convertible<F, ftype>::value &&
!std::is_same<RecursiveHelper, std::decay_t<F>>::value>>
RecursiveHelper( F&& f ) : func(std::forward<F>(f)) {}
对于C ++ 11,将something_t<...>
替换为typename something<...>::type
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.