boost::bind & boost::function with partial args

boost::bind & boost::function with partial args

本文关键字:boost partial args with bind function      更新时间:2023-10-16

我给你发了一个我想做什么的例子,用这种方式更容易解释

    void myPrinter(const char* text, int number){            
            printf("n%s %dn", text, number);
        }
    int main() {
        char *someText="test";        
       boost::function<void(int my_number)> functionWithSavedArgs = boost::bind(&myPrinter, someText, ?????);
       //then I have to call my function with saved args and give to it only variable "number" like:
       int myBeautifulNumber = 2012;
       functionWithSavedArgs(myBeautifulNumber);
       // echo: test 2012
     }

有什么想法吗?

跳过那个参数。

   boost::function<void(int my_number)> functionWithSavedArgs
        = boost::bind(&myPrinter, someText);

这只绑定第一个参数。

如果你只想绑定第二个,你需要一个占位符:

   boost::function<void(int my_number)> functionWithSavedArgs
         = boost::bind(&myPrinter, _1, someNumber);