驭风万里无垠

GCC4.4.0 & C++0x 初体验

闲来无事,翻看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
 9using namespace std;
10
11void func1(int i, int j, tr1::tuple<intintint> k)
12{
13    cout << "func1:" << i << "" << j << ""
14         << ", tuple param:[" << get<0>(k) << "," << get<1>(k)
15         << "," << get<2>(k) << "]" << endl;
16}

17
18
19void func2(int i, int j)
20{
21    cout << "func2: " << i << "" << j << endl;
22}
    
23
24void func3(int k)
25{
26    cout << "func3: " << k << endl;
27}

28
29struct 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
42int 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,  45)));
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, 12, _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:02, , tuple param:[3,4,5]
func2: 
10
func3: 
0
MyFunc1::memFun1 :
021
MyFunc1::memFun2 :
120
MyFunc1::memFun1 :
022
由于我的环境下,新版的litstdc++.so被安装在了/usr/local/lib64/下边,所以需要手工指定动态库的路径(export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH即可绕过/usr/lib/libstdc++.so).

估计这么奇妙的特性,进入工业应用还得不少时间吧,麻烦的标准化...

posted on 2009-05-16 22:24 skyscribe 阅读(1776) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜