GCC4.4.0 & C++0x 初體驗
閑來無事,翻看GNU的郵件列表,發(fā)現4.4.0版本已經發(fā)布一個月有余,其中最大的改進莫過于c++了(也許是我對c++的部分最為關注的緣故),ChangeLog里邊甚至專門列了一個網頁描述針對C++0x的支持特性,忍不住體驗一把。第一步要做的自然是手動編譯GCC的源代碼了,因為我沒有找到Debian版本的升級包,干脆自己下載,我只需要gcc-core和g++兩個包就可以了,一個25M,一個7M,下載倒是挺順利,幾分鐘就OK了,接下來就是編譯了。常見的源碼編譯步驟就OK了:



我遇到的是有兩個關于多處理器的開發(fā)庫依賴,apt-get很容易就安裝上去了。
編譯的過程就比較漫長了,我的Pentium D 2.8G Dual Core活生生忙活了一個小午休的時間,起來發(fā)現還沒編譯完,不過十分鐘之后就發(fā)現所有的就OK了。
TR1的庫,boost的示例比較好,其中第21章有詳細的列表和用法簡要說明。參照那個查了一下GCC的頭文件,在
/usr/local/include/c++/4.4.0/tr1/ 里邊:


























我比較熟悉和期待的是bind, function, auto, shared_ptr, mem_fn這幾個庫了,寫了個小例子驗證之:
1
// g++ -std=c++0x -o testC++0x testNewC++.cpp
2
3
#include <tr1/memory>
4
#include <tr1/functional>
5
#include <tr1/tuple>
6
#include <vector>
7
#include <iostream>
8
9
using namespace std;
10
11
void func1(int i, int j, tr1::tuple<int, int, int> k)
12
{
13
cout << "func1:" << i << ", " << j << ", "
14
<< ", tuple param:[" << get<0>(k) << "," << get<1>(k)
15
<< "," << get<2>(k) << "]" << endl;
16
}
17
18
19
void func2(int i, int j)
20
{
21
cout << "func2: " << i << ", " << j << endl;
22
}
23
24
void func3(int k)
25
{
26
cout << "func3: " << k << endl;
27
}
28
29
struct MyFunc1
30
{
31
void memFun1(int i, int j)
32
{
33
cout << "MyFunc1::memFun1 :" << i << ", " << j << endl;
34
}
35
36
void memFun2(int i, int j, int k)
37
{
38
cout << "MyFunc1::memFun2 :" << i << ", " << j << ", " << k << endl;
39
}
40
};
41
42
int main()
43
{
44
45
typedef tr1::function<void (int)> Func;
46
using std::tr1::bind;
47
using std::tr1::mem_fn;
48
using std::tr1::placeholders::_1;
49
using std::tr1::shared_ptr;
50
51
shared_ptr<MyFunc1> instPtr(new MyFunc1);
52
MyFunc1 functor;
53
54
vector<Func> funcs;
55
funcs.push_back(bind(&func1, _1, 2, tr1::make_tuple(3, 4, 5)));
56
funcs.push_back(bind(&func2, 1, _1));
57
funcs.push_back(&func3);
58
funcs.push_back(bind(&MyFunc1::memFun1, &functor, _1, 21));
59
funcs.push_back(bind(mem_fn(&MyFunc1::memFun2), &functor, 1, 2, _1));
60
funcs.push_back(bind(&MyFunc1::memFun1, instPtr, _1, 22));
61
62
for (auto it = funcs.begin(), itEnd = funcs.end();
63
it != itEnd; ++it)
64
{
65
(*it)(0);
66
}
67
68
return 0;
69
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

編譯之后,運行結果如下:






估計這么奇妙的特性,進入工業(yè)應用還得不少時間吧,麻煩的標準化...
posted on 2009-05-16 22:24 skyscribe 閱讀(1828) 評論(0) 編輯 收藏 引用 所屬分類: C++