return(x)并返回x。 [英] return(x) and return x.

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

问题描述

大家好..我有一个问题:返回(变量,指针......)和返回x的不同。 x:变量,指针......谢谢!!!

解决方案

(正如Richard所说)没有区别:http://msdn.microsoft.com/en-us/library/sta56yeb.aspx [ ^ ]



一些较旧的语言确实强制你必须使用带括号的括号,但你现在不太可能看到它们。

所以

 < span class =code-keyword> return  x; 





  return (x); 

与编译器完全相同。



需要括号是很少见的,因为在语句后没有任何内容可以添加,但是有些人确实将它添加为完整性 - 并且他们通常总是使用它们,即使它不是复合词返回表达式。但是,不建议这样做,因为它使return语句看起来像一个函数调用:

  int  x =  return  3 ); 

可能会混淆。

  int  x =  return   3 ; 

不能混淆。

(是的,是的,编译器会抱怨这两个,这只是一个例子......)


return语句中表达式周围的括号什么都不做,唯一的例外是decltype(auto) - 返回函数(它成为C ++ 14中C ++的一部分),括号中的括号与常规旧的decltype中的括号具有相同的含义。



  int  n; 
int f()
{
return n; // 返回int
// return(n);同样的事情
// return((((n))));同样的事情
}

decltype auto )f()
{
return n; // 返回int,就像decltype(n)
}
decltype auto )f()
{
返回(n); // 返回对int的引用,就像decltype((n))
}


hi all.. i have a question: the different of return(variable, pointer...) and return x. x: variable, pointer... thanks you!!!

解决方案

There is (as Richard has said) no difference: http://msdn.microsoft.com/en-us/library/sta56yeb.aspx[^]

Some older languages did enforce that you had to use parentheses with a return, but you are unlikely to see them now.
So

return x;


and

return (x);

are exactly the same as far as the compiler is concerned.

It's rare to need the parentheses as there is nothing you can add after the statement, but some people do add it for "completeleness" - and they generally always use them, even if it's not a compound return expression. However, it is discouraged as it makes the return statement look like a function call:

int x = return(3);

Can be confused.

int x = return 3;

Cannot be confused.
(Yes, yes, the compiler will complain about both of these, it's just an example...)


The parentheses around the expression in the return statement do exactly nothing, with the sole exception of decltype(auto)-returning functions (which became part of C++ in C++14) where the parentheses have the same meaning as the parentheses in a regular old decltype.

int n;
int f() 
{
   return n; // returns int
// return (n); same thing
// return ((((n)))); same thing
}

decltype(auto) f()
{
   return n; // returns int, just like decltype(n)
}
decltype(auto) f()
{
   return (n); // returns a reference to int, just like decltype((n))
}


这篇关于return(x)并返回x。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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