簡體   English   中英

std :: bind參數綁定到沒有對象的成員函數

[英]std::bind parameter to member function without object

我需要將一個參數綁定到類成員函數。 像這樣:

#include <functional>
#include <iostream>

struct test
{
    void func(int a, int b)
    {
        std::cout << a << " " << b << std::endl;
    }
};

int main(int argc, char** argv)
{
    typedef void (test::*TFunc)(int);
    TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}

但是在這種情況下,我有編譯錯誤

error: static assertion failed: Wrong number of arguments for pointer-to
-member

std::bind不會產生成員函數指針,但是會產生一個std::function對象,以后可以使用:

::std::function< void (test *, int)> func = std::bind(&test::func, std::placeholders::_1, 1, std::placeholders::_2);
test t{};
func(&t, 2);

暫無
暫無

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

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