1

I post you an example of what I want to do, that is easier to explain in this way

    void myPrinter(const char* text, int number){            
            printf("\n%s %d\n", 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
     }

Any ideas?

M4rk
  • 2,172
  • 5
  • 36
  • 70

1 Answers1

2

Just skip that argument.

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

This binds only the first argument.

If you wanted to bind only the second one, you would need a placeholder:

   boost::function<void(int my_number)> functionWithSavedArgs
         = boost::bind(&myPrinter, _1, someNumber);
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • I believe that the first bind operation should also use the placeholder, eg: boost::bind(&myPrinter, someText, _1); – Gearoid Murphy Feb 15 '12 at 16:35
  • I tried compiling the partial bind example without the placeholder and encountered dense compiler errors – Gearoid Murphy Feb 16 '12 at 13:45
  • 1
    From the Boost.bind documentation: `In a bind(f, a1, a2, ..., aN) expression, the function object f must be able to take exactly N arguments. [...] int f(int, int); boost::bind(f, 1); // error, f takes two arguments`. Unfortunately, you cannot omit arguments and expect them to be replaced by placeholders (it is the same with `std::bind` ; I don't really know why this is not possible). – Luc Touraille Jul 24 '12 at 13:00