免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2303 | 回复: 5
打印 上一主题 下一主题

[C++] g++中,一对象可以调用non-const成员,却无法被non-const引用,这种行为符合C++标准吗 [复制链接]

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-10 11:10 |只看该作者 |倒序浏览
30可用积分
在g++中,一个对象可以调用其non-const成员,却无法被non-const方式所引用,这种行为符合C++标准吗?
  1. struct A
  2. {
  3.     void memfun()
  4.     {
  5.     }

  6.     int val;
  7. };

  8. A foo( A& a )
  9. {
  10.     return a;
  11. }

  12. int main()
  13. {
  14.     //foo( A() ); // gcc4.7.2报:error: ……from an rvalue of type 'A'
  15.     A a;
  16.     foo(a).memfun(); // 既然是rvalue,为什么却可以调用non-const成员memfun?
  17.    
  18.     return 0;
  19. }
复制代码

最佳答案

查看完整内容

这是符合标准的,C++ 标准并没有说Rvalue一定是const的,也就是说rvalue有cv的修饰,2003标准中有如下规定:"Some rvalue expressions—those of class or cv-qualified class type—also refer to objects""Class rvalues can have cv-qualified types;"而对于rvalue对象调用成员函数有如下说明:“Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the impleme ...

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
2 [报告]
发表于 2013-07-10 11:10 |只看该作者
这是符合标准的,C++ 标准并没有说Rvalue一定是const的,也就是说rvalue有cv的修饰,2003标准中有如下规定:

"Some rvalue expressions—those of class or cv-qualified class type—also refer to objects"
"Class rvalues can have cv-qualified types;"

而对于rvalue对象调用成员函数有如下说明:
“Expressions such as invocations of constructors and of functions that return a class type refer to objects, and the implementation
can invoke a member function upon such objects, but the expressions are not lvalues.”
上面这句话说可以在作为rvalue(非lvalue既rvalue)表达式的(作为返回对象的函数结果值)情况下调用成员函数。且没有标准要求是const成员函数。

根据上述rvalue与cv的结合性及成员函数的调用说明上来讲g++的行为是合乎标准规定的。

而在VC下 foo( A() )编译没有问题是因为默认启用了Microsoft Extensions to C and C++。其有中这么一项:
Passing a Non-Const Pointer Parameter to a Function that Expects a Reference to a Const Pointer Parameter
因为引用是lvalue所以g++下编译不过是正常的。

可以参考:http://msdn.microsoft.com/en-us/library/34h23df8.aspx


网络上其它的相关说法:
Well, for an non-const rvalue, you can even call the
non-const member function for that object ... That means,
you can even *change* the rvalue if you want. Therefore,
obtaining the "this" pointer is not so strange compared
with these behaviors in my mind...



For non-class types, the const keyword is ignored for rvalues;
an rvalue of a non-class type is never "cv-qualified". (For
class types, it is relevant, since you can call member functions
on an rvalue; if the rvalue is const, you can only call const
member functions on it.) The return value of a function is an
rvalue, so there's really no point in declaring it const. (Note
that except for calling a member function on it, there is no
possible way to modify an rvalue.)

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
3 [报告]
发表于 2013-07-10 11:29 |只看该作者
A foo( const A& a ) 这样应该都可以了

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
4 [报告]
发表于 2013-07-10 12:08 |只看该作者
回复 2# hellioncu
我想知道C++标准是怎么规定的,因为VC9.0中 foo( A() ); 是可以编译通过的
   

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2013-07-10 12:51 |只看该作者

foo( A() ); ,参数非const方式时,不能通过编译,的确很不爽。

C++一定藏着一个羞于启齿的秘密,这个秘密违背了其标准的和谐。
这个秘密是什么呢??

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
6 [报告]
发表于 2013-07-10 15:09 |只看该作者
回复 2# myworkstation
谢谢,我记得当年有人回答过我(我忘了),当然,没你说得这么详细。再次感谢!

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP