【发布时间】:2015-05-23 10:13:42
【问题描述】:
我的代码如下:
#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;
}
我的编译器信息是: Apple LLVM 版本 6.0 (clang-600.0.56) (基于 LLVM 3.5svn) 目标:x86_64-apple-darwin13.4.0 线程模型:posix
【问题讨论】:
-
cppreference 说(关于
operator()的主题,以获取std::bind的返回值):如果在调用 g() 时提供的某些参数不匹配存储在 g 中的任何占位符,未使用的参数将被评估并丢弃。
标签: c++ function c++11 bind std