之间差我有以下代码:C++ 11绑定:使用和不使用占位符
1 #include <functional>
2 #include <iostream>
3
4 using namespace std::placeholders;
5
6 void update23(int i1, int &i2, int &i3)
7 {
8 i2 += i1;
9 i3 += i2;
10
11 std::cout << i2 << " " << i3 << std::endl;
12 }
13
19
20 int main()
21 {
22 int i1 = 10, i2 = 20, i3 = 30;
23
26 std::bind(update23, 10, 20, 30)();
27 std::bind(update23, 10, _1, _2)(20, 30);
28
29 return 0;
30 }
第27行中编译失败。失败是有道理的,因为常量被作为参考传递。
为什么26行通过?
我检查了C++绑定源,但很快就迷路了。
如果你可以引导我通过绑定源来理解这里的魔法是什么,将非常感激!
'的std :: bind'副本已绑定参数,并将它们作为左值稍后传递给回调。 – 0x499602D2
@Cthulhu它们在'' –
user2079303
中定义,代码为我编译得很好。似乎你的编译器无法优化它。我正在使用vs2012 – cageman