简体   繁体   English

如何获得从包含 std::function 的向量 std::vector 调用的函数的返回

[英]How can I get the return of a function called from a vector std::vector that contains std::function

I have a structure that in the constructor receives an initialization list std::initializer_list<P...> of type parameter pack, that constructor is filled with lambda functions and they are saved in a std::vector<P...> point is how I get the return of those functions when traversing the vector calling each function here I leave an example of the structure and what I want to do我有一个结构,在构造函数中接收类型参数包的初始化列表 std::initializer_list<P...>,该构造函数填充有 lambda 函数,它们保存在 std::vector<P...>重点是在遍历调用每个函数的向量时如何获得这些函数的返回 这里我留下了一个结构示例以及我想要做什么

 #include <iostream>
#include <functional>
#include <initializer_list>
using namespace std;

struct my_type {
    my_type(){}
    my_type(string _value) : value(_value) {}
    string value = "test";
    string getValue(){return value;}
};


template<typename A, class...P>
struct struct_name {
  struct_name(std::initializer_list<P...> list)  : functions(list) {}
  std::vector<P...> functions;
  
  string value;
  my_type type;      

  string execute_functions(){
     for (size_t i = 0; i < functions.size(); i++)
    {
     value = functions[i](type);  // something like this, this does not work
    }
    return value;
    std::cout << value;
  }
};

typedef struct_name<std::function<void(my_type)>, std::function<void(my_type)>> _functions;


int main(int argc, char const *argv[])
{

    _functions object = {
      [](my_type var)->string{
        return var.getValue();
      },
      [](my_type var)->string{
        return var.getValue();
      },
    };

    return 0;
}

everything works perfect except the way to obtain those values, I totally don't know and no matter how hard I look I can't find answers除了获得这些值的方式外,一切都很完美,我完全不知道,无论我看起来多么努力,我都找不到答案

edit: I can't paste the complete code because it depends on many other classes and I tried to recreate that section, the type is parameter pack because it receives multiple types besides lambdas but in the example I just put it that way编辑:我无法粘贴完整的代码,因为它依赖于许多其他类,我试图重新创建该部分,类型是参数包,因为它接收除了 lambdas 之外的多种类型,但在示例中我只是这样说

If you are trying to process a value through a series of function you can just use std::accumulate :如果您尝试通过一系列函数处理值,则可以使用std::accumulate

#include <iostream>
#include <vector>
#include <functional>
#include <numeric>

int main()
{
  std::vector<std::function<float(float)>> functions = {
    [] (float f) { return f*f; },
    [] (float f) { return f + 2; }
  };
    
  float result = std::accumulate(functions.begin(), functions.end(), 1.5f, 
    [] (float v, const auto& lambda) {
      return lambda(v); 
    }
  );
    
  std::cout << 1.5f << " -> " << result << std::endl;

  return 0;
}

But a vector can only contain one specified type, so what you are trying to do with you parameter pack P... doesn't make much sense, if you want to process multiple values through multiple functions with different signatures you'd better try with something like std::tuple<std::function<T1(T2)>, std::function<T3(T4)>, ...> and pass multiple values to it.但是一个向量只能包含一个指定的类型,所以你试图用你的参数包P...做的事情没有多大意义,如果你想通过具有不同签名的多个函数处理多个值,你最好尝试使用std::tuple<std::function<T1(T2)>, std::function<T3(T4)>, ...>之类的东西并将多个值传递给它。

You should be using std::tuple , if you want to store arbitrary callable objects.如果要存储任意可调用对象,则应该使用std::tuple

You can combine std::index_sequence with a fold expression for calling the functions:您可以将std::index_sequence与折叠表达式结合起来调用函数:

#include <iostream>
#include <string>
#include <tuple>
#include <utility>

using std::string;

struct my_type {
    my_type(){}
    my_type(string _value) : value(_value) {}
    string value = "test";
    string getValue(){return value;}
};

template<class...P>
struct struct_name {
    struct_name(P... args)
        : functions(args...)
    {}

    std::tuple<P...> functions;

    std::string value;
    my_type type;

    std::string execute_functions() {
        return execute_helper(std::make_index_sequence<sizeof...(P)>{});
    }
private:
    template<size_t ...Is>
    std::string execute_helper(std::index_sequence<Is...>)
    {
        ((value += std::get<Is>(functions)(type)), ...);
        return value;
    }
};

int main()
{
    struct_name foo(
        [](my_type var)->string {
            return var.getValue();
        },
        [](my_type var)->string {
            return var.getValue();
        });
    std::cout << foo.execute_functions() << '\n';
    return 0;
}

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

相关问题 来自std :: vector的函数组成<std::function> - Function composition from std::vector<std::function> 从返回 std::optional 的 std::vector 的函数中获取结果到调用者 - Get result into the caller from a function returning std::optional of std::vector 我的函数应该返回一个指向std :: vector的指针,还是对std :: vector的引用? - Should my function return a pointer to std::vector, or a reference to std::vector? 如何制作 function 指针的 std::vector? - How can I make a std::vector of function pointers? 如何为std :: vector专门化模板成员函数<T> - How can I specialize a template member function for std::vector<T> 如何管理std :: vector的std :: vector - How to manage std::vector of std::function C++ 从 std::vector 中删除 std::function <std::function<…> &gt; </std::function<…> - C++ removing an std::function from a std::vector<std::function<…>> 为什么我不能得到std :: function <std::vector<T> :: iterator&gt;绑定到返回该类型的lambda? - Why can't I get std::function<std::vector<T>::iterator> to bind to lambda returning that type? 如何从 std::vector 返回 object 的引用<std::vector<> &gt; </std::vector<> - how to return a reference of an object from std::vector<std::vector<>> 我可以分离std :: vector <char> 从它包含的数据? - Can I detach a std::vector<char> from the data it contains?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM