閑來無事,翻看GNU的郵件列表,發現4.4.0版本已經發布一個月有余,其中最大的改進莫過于c++了(也許是我對c++的部分最為關注的緣故),
ChangeLog里邊甚至專門列了一個
網頁描述針對C++0x的支持特性,忍不住體驗一把。
第一步要做的自然是手動編譯GCC的源代碼了,因為我沒有找到Debian版本的升級包,干脆自己下載,我只需要gcc-core和g++兩個包就可以了,一個25M,一個7M,下載倒是挺順利,幾分鐘就OK了,接下來就是編譯了。常見的源碼編譯步驟就OK了:
./Configure
make
make install
我遇到的是有兩個關于多處理器的開發庫依賴,apt-get很容易就安裝上去了。
編譯的過程就比較漫長了,我的Pentium D 2.8G Dual Core活生生忙活了一個小午休的時間,起來發現還沒編譯完,不過十分鐘之后就發現所有的就OK了。
TR1的庫,boost的示例比較好,其中
第21章有詳細的列表和用法簡要說明。參照那個查了一下GCC的頭文件,在
/usr/local/include/c++/4.4.0/tr1/ 里邊:
ls -lh | awk '$8 ~/^[a-z]+$/{print $8}'

array
ccomplex
cctype
cfenv
cfloat
cinttypes
climits
cmath
complex
cstdarg
cstdbool
cstdint
cstdio
cstdlib
ctgmath
ctime
cwchar
cwctype
functional
memory
random
regex
tuple
utility
我比較熟悉和期待的是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
}
編譯之后,運行結果如下:
func1:0, 2, , tuple param:[3,4,5]
func2: 1, 0
func3: 0
MyFunc1::memFun1 :0, 21
MyFunc1::memFun2 :1, 2, 0
MyFunc1::memFun1 :0, 22
由于我的環境下,新版的litstdc++.so被安裝在了/usr/local/lib64/下邊,所以需要手工指定動態庫的路徑(export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH即可繞過/usr/lib/libstdc++.so).
估計這么奇妙的特性,進入工業應用還得不少時間吧,麻煩的標準化...