jeudi 19 mars 2015

Why std::bind can be assigned to argument-mismatched std::function?


I have code as follows:



#include <functional>
#include <iostream>
using namespace std;
void F(int x) {
cout << x << endl;
}
int main() {
std::function<void(int)> f1 = std::bind(F, std::placeholders::_1);
f1(100); // This works, will print 100.

int x = 0;
std::function<void()> f2 = std::bind(F, x);
f2(); // This works, will print 0.

std::function<void(int)> f3 = std::bind(F, x);
f3(200); // BUT WHY THIS WORKS?????? It prints 0.
return 0;
}


My compiler info is: Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn) Target: x86_64-apple-darwin13.4.0 Thread model: posix




Aucun commentaire:

Enregistrer un commentaire