簡體   English   中英

轉換std :: function <void(string, string)> 到一個通用的std :: function <void()>

[英]Converting std::function<void(string, string)> to a generic std::function<void()>

我正在嘗試C ++ 0x的某些功能(在gcc 4.5下):

我知道有可能在編譯時指定參數時將std::function<void(string, string)>std::function<void()> 但是在運行時提交參數時可能嗎?

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

using namespace std;
using namespace placeholders;

class Print{

  public:
    void print1(string s1, string s2){ cout<<"s1 : "<<s1<<" s2 : "<<s2<<endl;}
    void print2(string s1){ cout<<"s1 : "<<s1<<endl;}

};

Print p  =  Print();

function<void(string, string)> f1(bind(&Print::print1, &p, _1, _2));

function<void()> f = f1;

我得到那些錯誤:

/usr/include/c++/4.5/functional:2103:6:   instantiated from ‘std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::function<void(std::basic_string<char>, std::basic_string<char>)>, _Res = void, _ArgTypes = {}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void()>::_Useless]’
../src/Cpp0x_test.cpp:345:34:   instantiated from here
/usr/include/c++/4.5/functional:1713:9: error: no match for call to ‘(std::function<void(std::basic_string<char>, std::basic_string<char>)>) ()’
/usr/include/c++/4.5/functional:2111:5: note: candidate is: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = void, _ArgTypes = {std::basic_string<char>, std::basic_string<char>}]

實際上我需要做的是:

function<void(string, string)> f1(bind(&Print::print1, &p, _1, _2));
function<void(string)> f2(bind(&Print::print2, &p, _1));

function<void()> fx1 = f1;
function<void()> fx2 = f2;

std::vector<function<void()> > vec;

vec.push_back(fx1);
vec.push_back(fx2);

//then, later

function<void()> call1 = vec[0];
function<void()> call2 = vec[1];

call1("test1", "test2");
call2("test3");

這個問題真的沒有道理。

我知道有可能在編譯時指定參數時將std::function<void(string, string)>std::function<void()> 但是在運行時提交參數時可能嗎?

如果您正在談論這樣做以將參數設置為“編譯時”:

string arg1,arg2;
function<void()> f = bind(f1,arg1,arg2); // f = [=] { f1(arg1,arg2); };

這實際上是在運行時進行綁定。 即使綁定是在運行時設置的,只要綁定時調用了這些參數具有的任何值,例如,從用戶輸入中調用f()都將使用這些運行時值。

也許您的意思是,上述代碼在調用bind時將f1綁定到arg1arg2的值,並且稍后更改bind使用的對象的值不會影響對f()調用中使用的值。 有一種解決方法:

string arg1,arg2;
function<void()> f =
  bind(f1,std::ref(arg1),std::ref(arg2)); // f = [&,f1] { f1(arg1,arg2); };

這導致f保留對對象的引用,而不僅僅是調用bind使用的靜態值。 現在,您可以為arg1arg2分配新值,並且在調用f()時將使用新值。 請注意,您必須確保f持有的引用保持有效,並且只要f仍可以被調用就不會成為懸掛引用。

function<void(string)> foo = [](string s){ cout << s; };
string arg = "Hello,";
function<void()> bar = bind(foo,ref(arg)); // bar = [=,&arg] { foo(arg); };
bar(); // prints "Hello,"
arg = " World!"
bar(); // prints " World!"

也可以使用bind:

string arg1, arg2;

function<void()> f(bind(f1, arg1, arg2));

f(); // calls f1(arg1, arg2) with their values at the time of bind

讓我們看看我是否理解您的要求。

為什么不將參數而不是函數存儲在向量中?

std::vector<std::tuple<std::string,std::string>> v;
v.push_back(std::make_tuple("a", "b")); // repeat

// Later that day...
for(auto& t : v) {
    f(get<0>(t), get<1>(t));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM