闲来无事,翻看GNU的邮件列表,发现4.4.0版本已经发布一个月有余,其中最大的改进莫过于c++了(也许是我对c++的部分最为关注的缘故),
ChangeLog里边甚至专门列了一个
网页描述针对C++0x的支持特性,忍不住体验一把。
第一步要做的自然是手动编译GCC的源代码了,因为我没有找到Debian版本的升级包,干脆自己下载,我只需要gcc-core和g++两个包就可以了,一个25M,一个7M,下载倒是挺顺利,几分钟就OK了,接下来就是编译了。常见的源码编译步骤就OK了:       
 ./Configure
./Configure
 make
make
 make install
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}'
ls -lh | awk '$8 ~/^[a-z]+$/{print $8}'

 array
array
 ccomplex
ccomplex
 cctype
cctype
 cfenv
cfenv
 cfloat
cfloat
 cinttypes
cinttypes
 climits
climits
 cmath
cmath
 complex
complex
 cstdarg
cstdarg
 cstdbool
cstdbool
 cstdint
cstdint
 cstdio
cstdio
 cstdlib
cstdlib
 ctgmath
ctgmath
 ctime
ctime
 cwchar
cwchar
 cwctype
cwctype
 functional
functional
 memory
memory
 random
random
 regex
regex
 tuple
tuple
 utility
utility我比较熟悉和期待的是bind, function, auto, shared_ptr, mem_fn这几个库了,写了个小例子验证之:
 1 // g++ -std=c++0x -o testC++0x testNewC++.cpp
// g++ -std=c++0x -o testC++0x testNewC++.cpp 
 2
 3 #include <tr1/memory>
#include <tr1/memory>
 4 #include <tr1/functional>
#include <tr1/functional>
 5 #include <tr1/tuple>
#include <tr1/tuple>
 6 #include <vector>
#include <vector>
 7 #include <iostream>
#include <iostream>
 8
 9 using namespace std;
using namespace std;
10
11 void func1(int i, int j, tr1::tuple<int, int, int> k)
void func1(int i, int j, tr1::tuple<int, int, int> k)
12

 {
{
13 cout << "func1:" << i << ", " << j << ", "
    cout << "func1:" << i << ", " << j << ", "
14 << ", tuple param:[" << get<0>(k) << "," << get<1>(k)
         << ", tuple param:[" << get<0>(k) << "," << get<1>(k)
15 << "," << get<2>(k) << "]" << endl;
         << "," << get<2>(k) << "]" << endl;
16 }
}
17
18
19 void func2(int i, int j)
void func2(int i, int j)
20

 {
{
21 cout << "func2: " << i << ", " << j << endl;
    cout << "func2: " << i << ", " << j << endl;
22 }
}    
23
24 void func3(int k)
void func3(int k)
25

 {
{
26 cout << "func3: " << k << endl;
    cout << "func3: " << k << endl;
27 }
}
28
29 struct MyFunc1
struct MyFunc1
30

 {
{
31 void memFun1(int i, int j)
    void memFun1(int i, int j)
32
 
     {
{
33 cout << "MyFunc1::memFun1 :" << i << ", " << j << endl;
        cout << "MyFunc1::memFun1 :" << i << ", " << j << endl;
34 }
    }
35
36 void memFun2(int i, int j, int k)
    void memFun2(int i, int j, int k)
37
 
     {
{
38 cout << "MyFunc1::memFun2 :" << i << ", " << j << ", " << k << endl;
        cout << "MyFunc1::memFun2 :" << i << ", " << j << ", " << k << endl;
39 }
    }
40 };
};
41
42 int main()
int main()
43

 {
{
44 
    
45 typedef tr1::function<void (int)> Func;
    typedef tr1::function<void (int)> Func;
46 using std::tr1::bind;
    using std::tr1::bind;
47 using std::tr1::mem_fn;
    using std::tr1::mem_fn;
48 using std::tr1::placeholders::_1;
    using std::tr1::placeholders::_1;
49 using std::tr1::shared_ptr;
    using std::tr1::shared_ptr;
50
51 shared_ptr<MyFunc1> instPtr(new MyFunc1);
    shared_ptr<MyFunc1> instPtr(new MyFunc1);
52 MyFunc1 functor;
    MyFunc1 functor;
53
54 vector<Func> funcs;
    vector<Func> funcs;
55 funcs.push_back(bind(&func1, _1, 2, tr1::make_tuple(3,  4, 5)));
    funcs.push_back(bind(&func1, _1, 2, tr1::make_tuple(3,  4, 5)));
56 funcs.push_back(bind(&func2, 1, _1));
    funcs.push_back(bind(&func2, 1, _1));
57 funcs.push_back(&func3);
    funcs.push_back(&func3);
58 funcs.push_back(bind(&MyFunc1::memFun1, &functor, _1, 21));
    funcs.push_back(bind(&MyFunc1::memFun1, &functor, _1, 21));
59 funcs.push_back(bind(mem_fn(&MyFunc1::memFun2), &functor, 1, 2, _1));
    funcs.push_back(bind(mem_fn(&MyFunc1::memFun2), &functor, 1, 2, _1));
60 funcs.push_back(bind(&MyFunc1::memFun1, instPtr, _1, 22));
    funcs.push_back(bind(&MyFunc1::memFun1, instPtr, _1, 22));
61
62 for (auto it = funcs.begin(), itEnd = funcs.end();
    for (auto it = funcs.begin(), itEnd = funcs.end();
63 it != itEnd; ++it)
        it != itEnd; ++it)
64
 
     {
{
65 (*it)(0);
        (*it)(0);
66 }
    }
67
68 return 0;
    return 0;
69 }
} 
编译之后,运行结果如下:
 func1:0, 2, , tuple param:[3,4,5]
func1:0, 2, , tuple param:[3,4,5]
 func2: 1, 0
func2: 1, 0
 func3: 0
func3: 0
 MyFunc1::memFun1 :0, 21
MyFunc1::memFun1 :0, 21
 MyFunc1::memFun2 :1, 2, 0
MyFunc1::memFun2 :1, 2, 0
 MyFunc1::memFun1 :0, 22
MyFunc1::memFun1 :0, 22
由于我的环境下,新版的litstdc++.so被安装在了/usr/local/lib64/下边,所以需要手工指定动态库的路径(export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH即可绕过/usr/lib/libstdc++.so).
估计这么奇妙的特性,进入工业应用还得不少时间吧,麻烦的标准化...