
return ( ... );
这与更受欢迎的不同:
return ... ;
特别是第一个版本返回包含该return语句的函数堆栈本地的东西的地址/引用.
既然为什么某些东西想要返回一个对那些在那时没有生命的东西的引用呢?
这个成语有什么用例?考虑到C 11和C 14的新流行语和功能,有不同的用法吗?
解决方法 前C 1y返回的括号版本是相同的,如果我们查看 C++11 draft standard节6.6跳转语句,返回的语法是:return Expressionopt ;
return braced-init-List ;
表达式可以是任何表达式,我们可以从5.1节中看到主表达式(强调我的前进):
A parenthesized Expression is a primary Expression whose type and
value are IDentical to those of the enclosed Expression. The presence
of parentheses does not affect whether the Expression is an lvalue.
The parenthesized Expression can be used in exactly the same contexts
as those where the enclosed Expression can be used,and with the same
meaning,except as otherwise indicated.
在C 1y中,我们可以使用delctype(auto)来推断返回类型,这可以改变这种情况,我们可以从draft C++1y standard第7.1.6.4节自动说明符中看到:
When a variable declared using a placeholder type is initialized,or a
return statement occurs in a function declared with a return type that
contains a placeholder type,the deduced return type or variable type
is determined from the type of its initializer.[…]
并包含以下示例:
auto x3a = i; // decltype(x3a) is int
decltype(auto) x3d = i; // decltype(x3d) is int
auto x4a = (i); // decltype(x4a) is int
decltype(auto) x4d = (i); // decltype(x4d) is int&
我们可以看到使用delctype(auto)和带括号的表达式时有区别.正在应用的规则来自第7.1.6.2节简单类型说明符第4段,其中说:
For an Expression e,the type denoted by decltype(e) is defined as follows:
并包括以下项目符号:
— if e is an unparenthesized ID-Expression or an unparenthesized class
member access (5.2.5),decltype(e) is the type of the entity named by
e. If there is no such entity,or if e names a set of overloaded
functions,the program is ill-formed;
和:
总结— otherwise,if e is an lvalue,decltype(e) is T&,where T is the type of e;
以上是内存溢出为你收集整理的C 11/14并返回(…)vs返回全部内容,希望文章能够帮你解决C 11/14并返回(…)vs返回所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)