posts - 18,  comments - 104,  trackbacks - 0

 

最近最大的新闻莫过于微软发布Visual Studio2010了,对c++的支持更进一步,其intellsence的解析也使用了和以前完全不同的方法(以前是靠编译器,现在是独立inellsence单元),番茄可能要被打入冷宫了。

Stephan T. Lavavej在Visual c++ Team Blog上发布了VC10对C++0x标准的支持情况,包括:
lambdas, auto, static_assert and rvalue references,他的博客是针对CTP版本的。

我下载安装了VC10 beta1 professional,感觉除了卡,没啥不好的,新的intellsence和Java,C#一样,即时分析代码,并在有语法错误的地方画上红线。但是好像intellsence的语法不支持C++0x的,在右值引用,Lambda的地方,也画红线。


对于Stephan T. Lavavej写的

Rvalue References: C++0x Features in VC10, Part 2

我并没有打算全部翻译,有兴趣可以读原文,我只把我认为重要的地方翻译出来,顺序也调整了一下,水平有限,附上原文。

C++03 3.10/1 says: "Every expression is either an lvalue or an rvalue."  It's important to remember that lvalueness versus rvalueness is a property of expressions, not of objects.
C++03 3.10/1 提到:"任何一个表达式,不是左值,就是右值",左值或者右值是针对表达式而言的,object没有左右值之分,应该时刻谨记这一点。

Both lvalues and rvalues can be either modifiable (non-const) or non-modifiable (const).  Here are examples:

 左值和右值都有const 和非const之分,下面是一个例子:

 1string one("cute");
 2
 3const string two("fluffy");
 4
 5string three() return "kittens"; }
 6
 7const string four() return "are an essential part of a healthy diet"; }
 8
 9 
10
11one;     // modifiable lvalue
12
13two;     // const lvalue
14
15three(); // modifiable rvalue
16
17four();  // const rvalue
18

Type& binds to modifiable lvalues (and can be used to observe and mutate them). 

Type& 只能绑定非const左值(可以观察和修改)。

const Type& binds to everything: modifiable lvalues, const lvalues, modifiable rvalues, and const rvalues (and can be used to observe them).

const Type& 可以绑定所有类型:非const左值,const左值,非const右值和const右值(只可以观察)。

上面这些都是03的标准,然而这对move语义和完美转发的实习,确实是一种障碍。关于move语义和完美转发,请看《C++0x漫谈》系列之:右值引用 或“move语意与完美转发”(上)《C++0x漫谈》系列之:右值引用 或“move语意与完美转发”(下)

move语义就是怎么能在非const右值销毁之前,将其中仍有用的资源窃取过来,以求高效。这就需要语言本身能识别非const右值,03中,语言本身不支持,只能靠入侵的方式实现。0x中,在语言层面上得到了支持,用&&表示非const右值。下面看看0x为了支持右值,进行了那些修正。

下面是关于函数左右值的重载决议:
1 .  The initialization rules have veto power.

初始化规则具有否决权(否决权指对所有候选函数,如果有参数根本不能转化,就放弃考虑,比如把一个const type转化为type)。

2 .  Lvalues strongly prefer binding to lvalue references, and rvalues strongly prefer binding to rvalue references.

左值优先绑定左值,右值优先绑定右值。

3 .  Modifiable expressions weakly prefer binding to modifiable references.

非const表达式趋向于绑定非const引用。

正常情况下:
 1#include <iostream>
 2
 3#include <ostream>
 4
 5#include <string>
 6
 7using namespace std;
 8
 9 
10
11void meow(string& s) {
12
13    cout << "meow(string&): " << s << endl;
14
15}

16
17 
18
19void meow(const string& s) {
20
21    cout << "meow(const string&): " << s << endl;
22
23}

24
25 
26
27void meow(string&& s) {
28
29    cout << "meow(string&&): " << s << endl;
30
31}

32
33 
34
35void meow(const string&& s) {
36
37    cout << "meow(const string&&): " << s << endl;
38
39}

40
41 
42
43string strange() {
44
45    return "strange()";
46
47}

48
49 
50
51const string charm() {
52
53    return "charm()";
54
55}

56
57 
58
59int main() {
60
61    string up("up");
62
63    const string down("down");
64
65 
66
67    meow(up);
68
69    meow(down);
70
71    meow(strange());
72
73    meow(charm());
74
75}

76
77 
78
79C:\Temp>cl /EHsc /nologo /W4 four_overloads.cpp
80
81four_overloads.cpp
82
83 
84//output
85
86meow(string&): up
87
88meow(const string&): down
89
90meow(string&&): strange()
91
92meow(const string&&): charm()
93
94

In practice, overloading on Type& , const Type& , Type&& , and const Type&& is not very useful.  A far more interesting overload set is const Type& and Type&& :

在实践中,重载所有的4个版本没有意义,而我们更倾向于使用const string& 和string&&:
例子:
 1C:\Temp>type two_overloads.cpp
 2
 3#include <iostream>
 4
 5#include <ostream>
 6
 7#include <string>
 8
 9using namespace std;
10
11 
12
13void purr(const string& s) {
14
15    cout << "purr(const string&): " << s << endl;
16
17}

18
19 
20
21void purr(string&& s) {
22
23    cout << "purr(string&&): " << s << endl;
24
25}

26
27 
28
29string strange() {
30
31    return "strange()";
32
33}

34
35 
36
37const string charm() {
38
39    return "charm()";
40
41}

42
43 
44
45int main() {
46
47    string up("up");
48
49    const string down("down");
50
51 
52
53    purr(up);
54
55    purr(down);
56
57    purr(strange());
58
59    purr(charm());
60
61}

62
63 
64
65C:\Temp>cl /EHsc /nologo /W4 two_overloads.cpp
66
67two_overloads.cpp
68
69 
70
71// output
72
73purr(const string&): up
74
75purr(const string&): down
76
77purr(string&&): strange()
78
79purr(const string&): charm()
80
81


For purr(up) , the initialization rules veto neither purr(const string&) nor purr(string&&) .  up is an lvalue, so it strongly prefers binding to the lvalue reference purr(const string&) .  up is modifiable, so it weakly prefers binding to the modifiable reference purr(string&&) .  The strongly preferred purr(const string&) wins.

对于 purr(up) ,决议(1)没有否决 purr(const string&) 和 purr(string&&) ,up是左值,所以依照决议(2),它优先绑定左值purr(const string&) ,依照决议(3),它趋向于绑定非const右值 purr(string&&) ,所以左值胜出:purr(const string&) 。

For purr(down) , the initialization rules veto purr(string&&) due to const correctness, so purr(const string&) wins by default.

对于For purr(down),决议(1)以为const否决了 purr(string&&) ,所以选择purr(const string&)。

For purr(strange()) , the initialization rules veto neither purr(const string&) nor purr(string&&) .  strange() is an rvalue, so it strongly prefers binding to the rvalue reference purr(string&&) .  strange() is modifiable, so it weakly prefers binding to the modifiable reference purr(string&&) .  The doubly preferred purr(string&&) wins.

 对于purr(strange()),决议(1)都没有否决,strange() 是右值,所以依照决议(2),优先绑定右值purr(string&&),依照决议(3),strange() 是非const,趋向于绑定非const,所以purr(string&&) 两票获胜。

For purr(charm()) , the initialization rules veto purr(string&&) due to const correctness, so purr(const string&) wins by default.

对于purr(charm()), 初始化决议(1)否决了非const的purr(string&&) ,所以选择purr(const string&) 。

先写这么多吧,明天再写。
posted on 2009-05-27 23:17 尹东斐 阅读(1770) 评论(3)  编辑 收藏 引用

FeedBack:
# re: Rvalue References: C++0x Features in VC10 (一)
2009-05-28 09:03 | 飘飘白云
我正在译言上翻译Stephan T. Lavavej这一系列的三篇文章,不过刚开始翻译第一篇,哈哈,你已经翻第二篇了,到时候借鉴了~~  回复  更多评论
  
# re: Rvalue References: C++0x Features in VC10 (一)
2009-05-28 16:37 | 尹东斐
@飘飘白云

我只是觉得这个rvalue reference是最重要的特性,可以不知不觉的增加效率,所以就决定写点什么。  回复  更多评论
  
# re: Rvalue References: C++0x Features in VC10 (一)[未登录]
2009-05-28 17:18 | king
高薪招聘:手机游戏开发等各类手机行业人才
职位描述: 手机游戏开发高级工程师
要求:
1.大专以上学历,做手机游戏开发至少三年以上。
2.熟悉J2ME和C++等工作专业知识

招聘:手机软件经理/资深工程师
职位描述:
1. 至少三年以上手机制造企业或相关企业专职开发工作经验,熟悉下列手机开发平台一种或多种:MTK,展讯,英飞凌,coolsand平台,google的android或其他开发平台
2. 熟悉 c/c++,对 java 有一定了解
3. 熟悉 MFC ,能够熟练使用 VC ,懂e-VC
4. 熟悉TCP/IP、HTTP,了解各种互联网应用(如browser,email,IM);

手机销售总监及经理(海外市场)
工作职责:
1.销售体系完善,销售策略优化。 2.销售团队管理,提升销售业绩。
具体要求如下:
1.五年以上手机海外销售相关经验,销售区域最好在东南亚,中东,非洲地区或其他发展中国家。
2.三年以上高级销售经理或总监管理经验
3. 英语流利沟通,或会法语或日语或其他外语的优先。

手机销售总监及经理(国内市场):
1.熟悉国内手机市场,销售模式及分销渠道
2.有一定的客户资源及代理商、分销售商资源
3.有销售战略意识,优秀的战术管理能力及团队领导能力

射频方向(6人 RF/RFID)
1 本科以上学历,电子或通信专业;
2 熟悉MSP430单片机,能独立负责项目研发工作;
3 熟悉和精通 cc1100 cc2500 cc1101等TI CHIPCON产品优先;
4 熟悉精通无线传感器网络,低速率低功耗无线个域网;
5 熟练精通C/C++程序设计,有多年软件开发经验者优先;
6 熟悉有源RFID,无源RFID者优先;
7 有丰富射频开发经验者优先考虑。

职位要求: 射频方向(6人 RF/RFID)
1 本科以上学历,电子或通信专业;
2 熟悉MSP430单片机,能独立负责项目研发工作;
3 熟悉和精通 cc1100 cc2500 cc1101等TI CHIPCON产品优先;
4 熟悉精通无线传感器网络,低速率低功耗无线个域网;
5 熟练精通C/C++程序设计,有多年软件开发经验者优先;
6 熟悉有源RFID,无源RFID者优先;
7 有丰富射频开发经验者优先考虑。

基带工程师
职位描述: 主要工作职责:
1、主要从事基带硬件电路设计、硬件调试、故障分析定位、客户硬件支持等工作;
2、从事音频调试工作;
3、从事物料评估、选型、认证等工作;
4、负责项目试产的技术支持,生产过程中硬件相关问题的现场解决。
职位要求: 任职要求:
1、学历:本科或以上学历;
2、专业:通讯、电子工程、计算机等相关专业;
3、工作经历:2年硬件开发工作经验,有3G开发经验者优先;
4、其他:项目涉及异地开发,需有良好的沟通能力,能适应出差,良好的英文读写能力。


备注: 本公司是专做手机行业的猎头公司,现为中国区域内多家知名手机企业招聘多名手机销售管理人才,根据资历定年薪,待遇优厚,上不封顶,工作地点根据候选人意向可协商(不限于深圳,上海,北京,广州,其他地区也有我公司客户需求)欢迎手机行业各类高级人才朋友联系我们(加QQ、MSN、手机),通过人才自荐或有奖推荐他人成为您职业生涯中的朋友。本招聘信息长期有效,联系人:king
联系电话:15889433717 QQ:877211649(手机行业) MSN:yclhr@yclhr.com
  回复  更多评论
  

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


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

常用链接

留言簿(4)

随笔档案

文章分类

文章档案

相册

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜