1

Below code confused me, I expected it can cause compiling error, but actually not. The stringmy string is silently dropped?

As I understand TaskFunction expetec two arguments, but when I use std::bind there is one placeholder. Why didn't compiler prevent me assign the bind result to TaskFunction varaible tf?

#include <string>
#include <functional>
#include <iostream>

typedef  std::function< void(int , const std::string&) > TaskFunction;


class MyClass {

public:
   void myTask(int i) {
        std::cout << __FUNCTION__ << " i = " << i << std::endl;
    }
};

int main()
{
    MyClass mc;
    TaskFunction tf = std::bind(&MyClass::myTask, &mc, std::placeholders::_1);
    tf(0, "my string");
    return 0;
}

Result:

myTask i = 0
bzhu
  • 908
  • 2
  • 9
  • 16

0 Answers0