简体   繁体   English

C ++ std :: function返回其自身类型的向量(再次是递归类型)

[英]C++ std::function that returns a vector of it's own type (recursive types again)

Ideally, I'd like to declare the following type: 理想情况下,我想声明以下类型:

using action_t = std::function< std::vector< action_t >(void) >

This is a thunk which returns a vector of followup thunks. 这是一个重击,返回了后续重击的向量。 I was able to get this far using information from Recursive typedef function definition : std::function returning its own type : 通过使用递归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>; 

However, to push these things onto a stack, for instance, I have to do things like this: 但是,例如,要将这些东西压入堆栈,我必须做这样的事情:

std::stack<action_t> stack;                                                                                                                            

stack.push(RecursiveHelper([&visitor, &node](void){                                                                                                    
    return visitor.visitNode(node);                                                                                                                    
    }));                                                                                                                                               

Ideally I'd like to avoid any mention of RecursiveHelper in code which uses this stuff, if they want a stack of action_t, they should be able to push conforming lambdas straight onto it. 理想情况下,我想避免在使用这种东西的代码中提及RecursiveHelper ,如果他们想要一堆action_t,他们应该能够将符合条件的lambda直接推入其中。

Is there any way to accomplish this? 有什么办法可以做到这一点?

Write a constructor that accepts any function object that is convertible to ftype and isn't RecursiveHelper : 编写一个接受任何可转换为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)) {}

For C++11, replace something_t<...> with typename something<...>::type . 对于C ++ 11,将something_t<...>替换为typename something<...>::type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM