建立一個匿名函數並執行。Objective-C採用的是上尖號^,而C++ 11採用的是配對的方括號[]。實例以下:ios
1
2
3
4
5
6
7
8
9
|
#include <iostream>
using
namespace
std;
int
main()
{
[]{
cout <<
"Hello,Worldn"
;
}();
}
|
咱們也能夠方便的將這個建立的匿名函數賦值出來調用:ide
1
2
3
4
5
6
7
8
9
10
11
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024;
auto
func = [](
int
i) {
// (int i) 是指傳入改匿名函數的參數
cout << i;
};
func(i);
}
|
1
2
3
4
5
6
7
8
9
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024;
auto
func = [] { cout << i; };
func();
}
|
vs 報錯
error C3493: 沒法隱式捕獲「i」,由於還沒有指定默認捕獲模式
error C2064: 項不會計算爲接受 0 個參數的函數wordpress
g++ 報錯:
error: ‘i’ is not captured函數
要直接沿用外部的變量須要在 [] 中指名捕獲。this
1
2
3
4
5
6
7
8
9
10
11
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024;
auto
func = [=]{
// [=] 代表將外部的全部變量拷貝一份到該函數內部
cout << i;
};
func();
}
|
結果:
1024spa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024;
auto
fun1 = [=]{
// fun1 內存在 i
cout << i;
// 1024
auto
fun2 = []{
// 未指名捕獲, i 不存在
cout << i;
};
fun2();
};
fun1();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024;
cout << &i << endl;
auto
fun1 = [&]{
cout << &i << endl;
};
fun1();
}
|
結果:
0x28ff0c
0x28ff0c指針
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024, j = 2048;
cout <<
"i:"
<< &i << endl;
cout <<
"j:"
<< &j << endl;
auto
fun1 = [=, &i]{
// 默認拷貝外部全部變量,但引用變量 i
cout <<
"i:"
<< &i << endl;
cout <<
"j:"
<< &j << endl;
};
fun1();
}
|
1
|
|
結果
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff0c
inside j:0x28ff04code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024, j = 2048;
cout <<
"outside i value:"
<< i <<
" addr:"
<< &i << endl;
auto
fun1 = [i]{
cout <<
"inside i value:"
<< i <<
" addr:"
<< &i << endl;
// cout << j << endl; // j 未捕獲
};
fun1();
}
|
結果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff04對象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024, j = 2048;
cout <<
"outside i value:"
<< i <<
" addr:"
<< &i << endl;
auto
fun1 = [&i]{
cout <<
"inside i value:"
<< i <<
" addr:"
<< &i << endl;
// cout << j << endl; // j 未捕獲
};
fun1();
}
|
結果:
outside i value:1024 addr:0x28ff08
inside i value:1024 addr:0x28ff08內存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream>
using
namespace
std;
int
main()
{
int
i = 1024, j = 2048, k;
cout <<
"outside i:"
<< &i << endl;
cout <<
"outside j:"
<< &j << endl;
auto
fun1 = [i, &j]{
cout <<
"inside i:"
<< &i << endl;
cout <<
"inside j:"
<< &j << endl;
// cout << k; // k 未捕獲
};
fun1();
}
|
結果:
outside i:0x28ff0c
outside j:0x28ff08
inside i:0x28ff00
inside j:0x28ff08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream>
using
namespace
std;
class
test
{
public
:
void
hello() {
cout <<
"test hello!n"
;
};
void
lambda() {
auto
fun = [
this
]{
// 捕獲了 this 指針
this
->hello();
// 這裏 this 調用的就是 class test 的對象了
};
fun();
}
};
int
main()
{
test t;
t.lambda();
}
|