右值 [英] rvalue

查看:98
本文介绍了右值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,已经有一段时间了!

我正在研究一个使用大量宏的错误代码。

A一堆看起来像

#define POSITION(mw,row)(mw.positions [row])

为了调试我要修改的东西这些macors所以它们只能用作rvalues而不是lvalues的
。如果它们被用作左值,我希望我的

compililation失败。不幸的是,我不记得一个很好的方法。

一如既往地感谢你的帮助!

Tobias。

Hi all, It''s been a while!
I''m working on a very buggy piece of code that uses a lot of macros.
A bunch of them look like
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I''d like to modify these macors so they can only be
used as rvalues and not as lvalues. If they are used as lvalues I want my
compililation to fail. Unfortunately I can''t remember a nice way to do that.
As always thanks for the help!
Tobias.

推荐答案

Stefan Ram写道:
Stefan Ram wrote:
Tobias Oed< to **** @ physics.odu.edu>写道:
Tobias Oed <to****@physics.odu.edu> writes:
为了调试我想要修改这些宏的东西,所以
它们只能用作右值而不是左值。
In order to debug the thing I''d like to modify these macors so
they can only be used as rvalues and not as lvalues.



您可以将它们转换为类型。 (演员阵容不会产生左/右值。)



You might cast them to their type. (A cast does not yield an
lvalue.)




True。但是我的编译器(gcc)在

之前需要特殊选项(-pedantic)以下失败


int main(void){

int a;

(int)a = 3;


返回0;

}


如果可能的话,我想避免这样做。另外,我必须要小心

修改所有这些宏,不要搞砸一下int

和short,我不擅长那个那种体力劳动。

前段时间有一个关于

网页的讨论,揭示了防御性编程的外部措施。一个

部分是上面的。当时我以为作者是疯了。

现在这只是我需要的东西但谷歌没有发现任何东西......

我我会尝试选择宏的演员表,直到出现更好的东西。

谢谢,托比亚斯。



True. But my compiler (gcc) requires special options (-pedantic) before
the following fails

int main(void){

int a;
(int) a = 3;

return 0;
}

If possible, I''d like to avoid that. Also, I''d have to be carefull
modifying all these macros not to screw up somewhere between an int
and short, and I''m no good at that kind of manual labour.
A while back there was a discussion here (or was it fclc?) about
web pages exposing exteme measures for defensive programming. One
part of it was the above. At the time I thought the author was insane.
Now that''s just about what I need but google doesn''t turn up anything...
I''ll try the cast for select macros until something better comes up.
Thanks, Tobias.


Tobias Oed写道:
Tobias Oed wrote:
Stefan Ram写道:
Stefan Ram wrote:
Tobias Oed< to **** @ physics.odu.edu>写道:
Tobias Oed <to****@physics.odu.edu> writes:
为了调试我想要修改这些宏的东西,所以
它们只能用作右值而不是左值。
In order to debug the thing I''d like to modify these macors so
they can only be used as rvalues and not as lvalues.



您可以将它们转换为类型。 (演员阵容不会产生左/右值。)



You might cast them to their type. (A cast does not yield an
lvalue.)



是的。但我的编译器(gcc)需要特殊选项(-pedantic)才能使以下内容失败

int main(void){

int a;
(int)a = 3;

返回0;
}
如果可能的话,我想避免这种情况。另外,我必须小心修改所有这些宏,而不是搞砸一下int
和short,我不擅长那种体力劳动。
前段时间有一个讨论(或者是fclc?)关于
网页暴露了防御性编程的外部措施。其中一部分就是上面的内容。当时我认为作者是疯了。
现在这只是我需要的东西,但谷歌没有发现任何东西......
我会尝试选择宏的演员表直到更好的东西出现。



True. But my compiler (gcc) requires special options (-pedantic) before
the following fails

int main(void){

int a;
(int) a = 3;

return 0;
}

If possible, I''d like to avoid that. Also, I''d have to be carefull
modifying all these macros not to screw up somewhere between an int
and short, and I''m no good at that kind of manual labour.
A while back there was a discussion here (or was it fclc?) about
web pages exposing exteme measures for defensive programming. One
part of it was the above. At the time I thought the author was insane.
Now that''s just about what I need but google doesn''t turn up anything...
I''ll try the cast for select macros until something better comes up.




好​​吧,所以我的谷歌很脏!
http://users.bestweb.net/~ctips/

他推荐像
$ b这样的东西$ b((void)0,a)= 3;

在给出警告之前gcc还需要-pedantic而不是

你的强制转换为类型技巧。

还有其他想法吗?

Tobias。




All right, so my googles are dirty!
http://users.bestweb.net/~ctips/
He recomends something like
((void) 0, a) = 3;
For which gcc also needs -pedantic before giving a warning instead of
an error with your cast to type trick.
Any other idea?
Tobias.



Tobias Oed写道:
Tobias Oed wrote:
#define POSITION(mw,row)(mw.positions [row])
为了调试我要修改这些宏的东西,所以他们
只能用作右值而不能用作左值。
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I''d like to modify these macors so they
can only be used as rvalues and not as lvalues.




#define RVALUE(x)((x)+0)

#def ine POSITION(mw,row)RVALUE(mw.positions [row])


或者你可以在你的宏周围撒上''+ 0'而不是使用

RVALUE宏,但之后读取代码的人可能想知道为什么你这么做是为了



-

Hallvard



#define RVALUE(x) ((x)+0)
#define POSITION(mw,row) RVALUE(mw.positions[row])

Or you could just sprinkle ''+0'' around your macros instead of using an
RVALUE macro, but then someone who reads the code later might wonder why
you have been doing so.

--
Hallvard


这篇关于右值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆