C++11 将参数传递给函数向量的std::function时出现问题

C++11 将参数传递给函数向量的std::function时出现问题,c++11,vector,arguments,std-function,stdbind,C++11,Vector,Arguments,Std Function,Stdbind,我试图创建std::function的向量,然后将该向量传递给函数。我还需要将参数传递给函数对象,所以我使用std::bind。代码如下: #include <functional> #include <iostream> #include <string> #include <vector> using namespace std; void execute(vector<function<void (int)>>&a

我试图创建std::function的向量,然后将该向量传递给函数。我还需要将参数传递给函数对象,所以我使用std::bind。代码如下:

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

using namespace std;

void execute(vector<function<void (int)>>& fs) {
    for (auto& f : fs) 
        f();
}

void func(int k) {
    cout << "In func " << k << endl;
}

int main()
{
    int i = 1;
    vector<function<void (int)>> x;
    auto f1 = bind(func, i);
    //f1(); // this does call intended function
    x.push_back(f1);

    execute(x);
}
但这会产生以下错误:

function_tmpl.cpp: In function ‘void execute(std::vector >&)’: function_tmpl.cpp:14:5: error: no match for call to ‘(std::function) ()’ f(); ^ In file included from function_tmpl.cpp:1:0: /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2142:11: note: candidate is: class function ^ /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2434:5: note: _Res std::function::operator()(_ArgTypes ...) const [with _Res = void; _ArgTypes = {int}] function:: ^ /usr/lib/gcc/x86_64-pc-cygwin/4.9.2/include/c++/functional:2434:5: note: candidate expects 1 argument, 0 provided 如果我在main中调用f,那么效果很好,这意味着函数已经绑定了参数,但是当作为参数传递给另一个函数时,它就不起作用了 功能, 因此编译器希望您提供一个参数,如下所示:
f1

您使用的是一个带单个int参数的void函数向量:vector,但实际上是在推void函数。您需要做的就是将向量的元素类型更改为vector。Bind的工作原理大致如下:

鉴于:

void f1(int i) { printf("%d", i); }
bindf1,1返回一个新函数f2:


因为你在按f2键,向量应该存储voidvoid函数。

绑定后,函数类型变成void。所以把向量的类型改成向量,你就会得到它

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

using namespace std;

void execute(vector<function<void ()>>& fs) {
    for (auto& f : fs) 
        f();
}

void func(int k) {
    cout << "In func " << k << endl;
}

int main()
{
    int i = 1;
    vector<function<void ()>> x;
    auto f1 = bind(func, i);
    x.push_back(f1);

    execute(x);
}
未指定std::bind的返回类型。因此,您不能期望std::bind返回与std::function相同类型的变量。使用decltype和模板来解析


函数仅接受接受整数的函数。调用f时缺少整数。请将其更改为函数或使用f0。@nwp,std::bind是否将参数绑定到函数对象?确实如此,但函数仍然需要整数。将整数绑定到函数后,绑定的函数不再需要整数,现在它有了签名函数。@nwp那么在这种情况下如何传递参数呢?将其更改为function不允许传递有界函数,并且f0将不起作用?将int绑定到函数时传递参数。如果绑定函数时不知道该函数应该具有的参数,则不应绑定该函数,返回函数并为要执行的参数传递一个向量,并执行类似于fs[i]arguments[i]的操作。@ZunTzu,std::bind是否会将参数绑定到函数对象?这对f1是正确的,但对f不是,静态定义为接受一个参数。@perreal我不是在推void函数。函数被限制为int参数:auto f1=bindfunc,i;正确,但向量的元素类型不正确,因为您正在推送不带参数的绑定函数。因此,您正在推送的函数的类型与原始函数不同。@perreal可能是我缺少了什么,但绑定函数(即func)在编辑中使用参数?@perreal+1作为绑定解释。这带来了清晰。好的是,我可以将函子和lambda函数也推到同一个向量中。这解决了一个传递参数的问题。但是,我不想推向量中的普通函数,我想推函子和lambda函数。所以我不能使用decltype来获取类型。如何解决这个问题?请参阅@songyuanyao的答案,了解如何使用模板实现这一点。示例:或者您想编写“不带”模板?是的,不带模板。
#include <functional>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void execute(vector<function<void ()>>& fs) {
    for (auto& f : fs) 
        f();
}

void func(int k) {
    cout << "In func " << k << endl;
}

int main()
{
    int i = 1;
    vector<function<void ()>> x;
    auto f1 = bind(func, i);
    x.push_back(f1);

    execute(x);
}
In func 1
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T>
void execute(vector<T>& fs) {
    for (auto& f : fs) 
        f();
}

void func(int k) {
    cout << "In func " << k << endl;
}

int main()
{
    int i = 1;
    auto f1 = bind(func, i);
    vector<decltype(f1)> x; //deduce type of f1
    x.push_back(f1);
    execute(x);
}