閑來(lái)無(wú)事,翻看GNU的郵件列表,發(fā)現(xiàn)4.4.0版本已經(jīng)發(fā)布一個(gè)月有余,其中最大的改進(jìn)莫過(guò)于c++了(也許是我對(duì)c++的部分最為關(guān)注的緣故),
ChangeLog里邊甚至專門列了一個(gè)
網(wǎng)頁(yè)描述針對(duì)C++0x的支持特性,忍不住體驗(yàn)一把。
第一步要做的自然是手動(dòng)編譯GCC的源代碼了,因?yàn)槲覜](méi)有找到Debian版本的升級(jí)包,干脆自己下載,我只需要gcc-core和g++兩個(gè)包就可以了,一個(gè)25M,一個(gè)7M,下載倒是挺順利,幾分鐘就OK了,接下來(lái)就是編譯了。常見(jiàn)的源碼編譯步驟就OK了:
./Configure
make
make install
我遇到的是有兩個(gè)關(guān)于多處理器的開(kāi)發(fā)庫(kù)依賴,apt-get很容易就安裝上去了。
編譯的過(guò)程就比較漫長(zhǎng)了,我的Pentium D 2.8G Dual Core活生生忙活了一個(gè)小午休的時(shí)間,起來(lái)發(fā)現(xiàn)還沒(méi)編譯完,不過(guò)十分鐘之后就發(fā)現(xiàn)所有的就OK了。
TR1的庫(kù),boost的示例比較好,其中
第21章有詳細(xì)的列表和用法簡(jiǎn)要說(shuō)明。參照那個(gè)查了一下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這幾個(gè)庫(kù)了,寫(xiě)了個(gè)小例子驗(yàn)證之:
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
}
編譯之后,運(yùn)行結(jié)果如下:
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
由于我的環(huán)境下,新版的litstdc++.so被安裝在了/usr/local/lib64/下邊,所以需要手工指定動(dòng)態(tài)庫(kù)的路徑(export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH即可繞過(guò)/usr/lib/libstdc++.so).
估計(jì)這么奇妙的特性,進(jìn)入工業(yè)應(yīng)用還得不少時(shí)間吧,麻煩的標(biāo)準(zhǔn)化...