测试值在条件表达式中的位置

测试值在条件表达式中的位置

本文介绍了测试值在条件表达式中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我注意到,在许多C和C ++代码中,许多程序员似乎都希望将测试值放在第一位条件表达式。

即,他们宁愿写这个:


if(-1 == foobar())


比这个:


if(foobar()== -1)


第二种形式看起来更自然,更容易阅读恕我直言,是我一直使用的

。但是,鉴于第一个

的高发生率,我想知道使用这种方式的含义。


谢谢,


-

NeyAndrédeMello Zunino

Hello.

I have noticed, in a lot of C and C++ code, that many programmers seem
to prefer putting the test values first in conditional expressions.
I.e., they would rather write this:

if (-1 == foobar())

than this:

if (foobar() == -1)

The second form looks more natural and easier to read IMHO and is the
one I have always used. However, given the high ocurrence of the first
one, I would like to know the implications of using one way or the other.

Thank you,

--
Ney André de Mello Zunino

推荐答案




编译器无论如何都不关心。除了一个C ++函数返回对非const对象的引用的

情况之外,

没有收益。


但是当对常量表达式进行相等测试时,请考虑

这个:


int x;

//分配一些值到x

if(x == 3)

//做点什么


现在考虑如果程序员提交的话会发生什么并非罕见

如果打算''=='',则只输入''=''错误。结果代码:


if(x = 3)

//做点什么


....是完全合法的,并具有评估分配给x的常数

值的效果。如果常量值为0,则测试始终为

false,并且始终跳过条件代码。如果常量是
而不是0,则测试始终为真且条件代码始终执行




另一方面,如果你总是写:


如果(3 == x)


....那么如果你不小心离开了第二个''=''结果是一个

约束违规,要求编译器发出诊断信息。


-

Jack Klein

主页:

comp.lang.c ++



The compiler does not care, one way or the other. And except for the
case of a C++ function returning a reference to a non-const object,
there is no gain.

But when doing equality tests against constant expressions, consider
this:

int x;
// assign some value to x
if (x == 3)
// do something

Now consider what happens if the programmer commits the not uncommon
error of typing only ''='' when ''=='' was intended. The resulting code:

if (x = 3)
// do something

....is perfectly legal, and has the effect of evaluating the constant
value assigned to x. If the constant value is 0, the test is always
false and the conditional code is always skipped. If the constant is
not 0, the test is always true and the conditional code is always
executed.

On the other hand, if you always write:

if (3 == x)

....then if you accidentally leave off the second ''='' the result is a
constraint violation requiring the compiler to issue a diagnostic.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




main.cc:在函数`int main(int,char **)''中:

main.cc:6:错误:分配时非左值


如果输入赋值运算符,则会得到诊断

而不是比较运算符因为foobar(void)

确实*不*返回左值。

这可能只是* force-of-habit *的一个例子。

使用t的程序员他的习惯习惯于在比较的左侧看到常数

,感觉更舒服

写*所有*比较表达式

即使它们不包含左值。


main.cc: In function `int main(int, char**)'':
main.cc:6: error: non-lvalue in assignment

You get a diagnostic if you type the assignment operator
instead of the comparison operator because foobar(void)
does *not* return an lvalue.
This is probably just an example of *force-of-habit*.
Programmers who use this trick get used to seeing the constant
on the left-hand side of a comparison and feel more comfortable
writing *all* comparison expressions that way
even if they don''t contain an lvalue.




非常聪明!

-JKop


Very clever indeed!
-JKop


这篇关于测试值在条件表达式中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 03:23