括号将返回值 - 为什么? [英] Parentheses around return values - why?

查看:117
本文介绍了括号将返回值 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候我看到code这样的(C,C ++和Java的有时):

Quite often I see code like this (C, C++ and sometimes Java):

return (value);

我没有看到这些括号的任何好处。所以我的问题是,有程序员假设收益是某种函数的返回值作为参数还是有真当这些括号有意义案件?

I don't see any benefit of these parentheses. So my question is, have the programmers assumed return to be some kind of function with the return value as argument or are there really cases where these parentheses make sense?

据我所知,类似的问题已经被问这里,但是这关系到只有ANSI C。我不知道是否有++还没有得到答复有专门针对C方面或Java。

I understand that a similar question has already been asked here, but this is related to ANSI C only. I wonder if there are aspects specific to C++ or Java that have not been answered there.

推荐答案

对于到C

括号放在那里是一个前pression和一个想要返回值是前pression的该值。 均匀则不需要括号。这是完全可以写类似

Parentheses are put where there is an expression and one wants the return value to be that value of the expression. Even then parentheses are not needed. It is completely ok to write something like

return x + y;

程序员不让它收益率(X + Y); 来使其更具可读性。

因此​​,把括号是舆论和实践的问题。

So, putting parentheses is a matter of opinion and practice.



对于C ++


With respect to C++

有就是括号事项一个神秘的情况下。引用<一个href=\"http://stackoverflow.com/questions/4762662/are-parentheses-around-the-result-significant-in-a-return-statement\">this问题

There is an arcane case where parentheses matters. Quoting this question

int var1 = 42;
decltype(auto) func1() { return var1; } // return type is int, same as decltype(var1)
decltype(auto) func1() { return(var1); } // return type is int&, same as decltype((var1))

可以看到返回值是不同的,这是由于该括号。你可以通过详细解答理解了。

You can see the returned values are different, and that is due to the parentheses. You can go through this answer to understand in detail.



对于Java中,括号中没有任何区别。


With respect to java, parentheses does not make any difference.

编码约定,建议去与

return x + y;

了解更多,请阅读这个答案。



注意:我不知道很多关于Java和C ++。在我的关于Java和C ++的回答内容都是从其他的答案服用。我这样做是为了巩固不同约定的三种语言。


NOTE: I do not know much about java and C++. All of the content in my answer about java and C++ are taken from other answers. I did this to consolidate the different conventions for the three languages.

这篇关于括号将返回值 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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