Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通用回调_C++_Templates_Generics_Callback_C++11 - Fatal编程技术网

C++ 通用回调

C++ 通用回调,c++,templates,generics,callback,c++11,C++,Templates,Generics,Callback,C++11,我正在尝试在类中存储回调。目前,我做了如下工作: struct Callback { Callback (std::function<void ()> func) : func_ (func){} void call() const { func(); } private: std::function<void ()> func_; }; 我想用一个参数包来存储参数,用一个typename来存储返回类型,然后做一个std::function c

我正在尝试在类中存储回调。目前,我做了如下工作:

struct Callback {
    Callback (std::function<void ()> func) : func_ (func){}
    void call() const { func(); }

private:
    std::function<void ()> func_;
};
我想用一个参数包来存储参数,用一个
typename
来存储返回类型,然后做一个
std::function callback \
之类的东西,用类似
callback \(givenArgs…)的东西来调用它。然而,我对模板的了解还不够,甚至不知道这是否可行

我从中得到的真正用途(至少现在)是计时器,但也许制作一个小型的
generic_函数
类来包装
std::function
会更有帮助。但在本例中,每2秒暂停和取消暂停的计时器:

void togglePause (Countdown &c) {
    c.togglePause();
}

int main() {
    Countdown main (10000); //create countdown of 10s
    Countdown delay (2000, togglePause, main); //this one calls func when expired

    for (; !main.expired();) { //go while unpaused time < 10s
        delay.wait().reset(); //wait 2s, call callback, reset back to 2s
    }
}
void切换暂停(倒计时和c){
c、 切换暂停();
}
int main(){
倒计时main(10000);//创建10秒倒计时
倒计时延迟(2000,togglePause,main);//这个在过期时调用func
对于(;!main.expired();){//在未暂停时间<10s时继续
delay.wait().reset();//等待2s,调用回调,重置回2s
}
}

当然,这也可以应用于其他概念,但我不确定首先如何获得这种语法。如果返回类型是
void
,我可以构建两种不同的形式,与前面一个无关的问题相比,这很好,但是存储具有任意数量和类型的参数的函数让我感到困惑。如果可能的话,我如何使用这样的语法?如果没有,语法会有多接近?

我认为您只需要使用std::bind将带参数的函数转换为不带参数的函数:

int main() {
    int someNum = 5;
    std::function<void (void)> boundFunc = std::bind(increment, std::ref(someNum));
    Callback callback (boundFunc); //will call `increment (someNum);`
}
intmain(){
int-someNum=5;
std::function boundFunc=std::bind(增量,std::ref(someNum));
Callback Callback(boundFunc);//将调用`increment(someNum)`
}

请注意,您需要std::ref来确保someNum通过引用传递,并且需要确保someNum在作用域中停留的时间长于回调。

我认为您只需要使用std::bind将带参数的函数转换为不带参数的函数:

int main() {
    int someNum = 5;
    std::function<void (void)> boundFunc = std::bind(increment, std::ref(someNum));
    Callback callback (boundFunc); //will call `increment (someNum);`
}
intmain(){
int-someNum=5;
std::function boundFunc=std::bind(增量,std::ref(someNum));
Callback Callback(boundFunc);//将调用`increment(someNum)`
}

请注意,您需要std::ref来确保someNum是通过引用传递的,并且您需要确保someNum在作用域中停留的时间比回调的时间长。

我一直在走这条路,但不是使用C++11(实际上,这会使它更容易)。“小心!”卡梅隆,谢谢你的提醒。我必须注意这一点,因为毫无疑问,我会尝试使用winapi函数来实现这一点,并且会被uber搞糊涂。你的平台是什么?(MSVC++?GCC?x86或x64?)请参阅我的更新答案:我错过了一个std::refI。我一直在走这条路,但没有使用C++11(实际上,这应该会让它更容易)。“小心!”卡梅隆,谢谢你的提醒。我必须注意这一点,因为毫无疑问,我会尝试使用winapi函数来实现这一点,并且会被uber搞糊涂。你的平台是什么?(MSVC++?GCC?x86或x64?)请参阅我的更新答案:我缺少了一个std::refIt,看起来应该可以工作(想法不错),但不幸的是它没有工作。不过,可能是我。看到我最新的问题了。是的,当我看到这一点时,我非常大声地呻吟。它与测试用例完美配合,所以我可以假设在实现它时它会。非常感谢。这比我想的要简单得多。我总是想方设法找到最乏味的做事方式。看起来应该行得通(好的想法),但不幸的是不行。不过,可能是我。看到我最新的问题了。是的,当我看到这一点时,我非常大声地呻吟。它与测试用例完美配合,所以我可以假设在实现它时它会。非常感谢。这比我想的要简单得多。我总是设法找到最乏味的做事方式。