[英]return (a) vs. return a
I have seen both in the C and C++ code I have been looking at. 我在C和C ++代码中都看到了我一直在关注的内容。
What is the difference? 有什么不同?
No difference at all. 没有任何区别。
The official syntax is return something;
官方语法是
return something;
or return;
或
return;
and of course it is a keyword, not a function. 当然它是一个关键字,而不是一个功能。
For this reason you should not read it as return( a );
因此,您不应将其视为
return( a );
but as return (a);
但作为
return (a);
I think the difference is subtle but clear, parentheses will not apply to return but to a. 我认为区别是微妙但明确,括号不适用于返回,而是适用于。
((((a))))
is the same as (a)
that is the same as a
. ((((a))))
与(a)
与a
相同。
You can also write something like... 你也可以写点像......
int x = (((100)));
You can also write something like... 你也可以写点像......
printf("%d\n", (z));
As someone said in the comments, there is now, with C++11 (2011 version of the C++ language) the new operator decltype
. 正如有人在评论中所说,现在,使用C ++ 11(2011版C ++语言)新的运算符
decltype
。 This operator introduces a new example where (a)
is different from a
, this is quite esoteric and a little out of topic but I add this example just for the purpose of completeness. 这个运算符引入了一个新的例子,其中
(a)
与a
不同,这是非常深奥的,有点偏离主题,但我添加这个例子只是为了完整性。
int x = 10;
decltype(x) y = x; // this means int y = x;
decltype((x)) z = x; // this means int& z = x;
y = 20;
z = 30;
std::cout << x << " " << y << " " << z << std::endl;
// this will print out "30 20 30"
Students will not be interested in this, as I said, too esoteric, and it will work only with compilers that supports at least part of the C++11 standard (like GCC 4.5+ and Visual Studio 2010). 正如我所说,学生们对此并不感兴趣太过深奥,而且它只能用于支持至少部分C ++ 11标准的编译器(如GCC 4.5+和Visual Studio 2010)。
This goes in contrast also with the use of typeid
keyword: 这与使用
typeid
关键字形成鲜明对比:
int a;
std::cout << typeid(a).name() << std::endl; // will print "int"
std::cout << typeid((a)).name() << std::endl; // will print "int" !!!!
Writing return x
indicates a programmer who understands what return
means. 写入
return x
表示程序员理解return
含义。 Whereas return(x)
indicates a programmer who incorrectly believes that return
is a kind of function. 而
return(x)
表示程序员错误地认为return
是一种功能。
return
is not a function. return
不是一个功能。
It is more a point of style. 这更像是一种风格。 I personally do not use parentheses in a return statement unless it is showing order of operations.
我个人不会在return语句中使用括号,除非它显示操作顺序。
examples 例子
return a;
return (a || b);
return (a && (b || c));
return (a ? b : c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.