简体   繁体   English

c ++:带有任意签名的std :: function的std :: vector

[英]c++: std::vector of std::function with arbitrary signatures

Is it possible to create an std::vector that can hold an std::function with any signature? 是否可以创建一个std::vector ,它可以保存带有任何签名的std::function (The function arguments would all be pre-bound.) (函数参数都将被预先绑定。)

I tried std::vector<std::function<void()> > , since if I only have one std::function of that type I can bind any function to it. 我尝试了std::vector<std::function<void()> > ,因为如果我只有一个该类型的std::function ,我可以将任何函数绑定到它。

This does not seem to work inside a vector: if I try to add a function with std::bind to the vector that has a signature other than void() , I get: 这似乎不适用于向量:如果我尝试将具有std::bind的函数添加到具有除void()之外的签名的向量,我得到:

No matching member function for call to 'push_back'

Is there a way to do this? 有没有办法做到这一点?

Edit: 编辑:

I just remembered that std::function<void()> does allow you to bind any function that returns void as long as the arguments are pre-bound with std::bind , it does not allow you to bind any signature though, but for my purposes it's generic enough, so the following works: 我记得std::function<void()>允许你绑定任何返回void函数,只要参数与std::bind预先std::bind ,它不允许你绑定任何签名,但是为了我的目的,它足够通用,所以以下工作:

class A
{
public:
    void test(int _a){ return 0; };
};

A a;
std::vector<std::function<void()> > funcArray;
funcArray.push_back(std::bind(&A::test, std::ref(a), 0));

This should work: 这应该工作:

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

void hello() { std::cout << "Hello\n"; }
void hello2(const char * name) { std::cout << "Hello " << name << '\n'; }

int main()
{

    std::vector<std::function<void()>> v;
    v.push_back(hello);
    v.push_back(std::bind(hello2, "tim"));
    v[0]();
    v[1]();
}

How are you doing it? 你好吗?

Is it possible to create an std::vector that can hold an std::function with any signature? 是否可以创建一个std :: vector,它可以保存带有任何签名的std :: function? (The function arguments would all be pre-bound.) (函数参数都将被预先绑定。)

So you're wondering if you can use a vector to hold objects of different types except that you've wrapped them all up in a single type? 所以你想知道你是否可以使用向量来保存不同类型的对象,除非你把它们全部包装在一个类型中?

Yes, you can store a bunch of items that all have the same type in a vector. 是的,您可以存储一组在矢量中具有相同类型的项目。


Or maybe you're trying to ask "How do I wrap callable objects such that they'll have the type I need?" 或者你可能试图问“我如何包装可调用的对象,以便它们具有我需要的类型?”

bind can handle the special case where you only need to remove or reorder parameters in order to get something convertible to the common type. bind可以处理特殊情况,你只需要删除或重新排序参数,以获得可转换为公共类型的东西。 In order to handle the general case where you may need choose the return type, add parameters, etc., you simply have to write new functions. 为了处理您可能需要选择返回类型,添加参数等的一般情况,您只需编写新函数即可。 Lambda's can make it easy to do that inline: Lambda可以很容易地进行内联:

std::vector<std::function<common_signature> F;

double foo(std::string);
// for a common signature of void()
F.push_back([]{ foo("hello"); });

int bar(void);
// for a common signature of int (int)
F.push_back( [](int i) { return bar() + i; });

int baz(void);
// for a common signature of std::complex<double> (void)
F.push_back( []() -> std::complex<double> { return {0,baz()}; });

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

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