C ++ 11/14和return(...)vs return [英] C++11/14 and return( ... ) vs return

查看:143
本文介绍了C ++ 11/14和return(...)vs return的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 中,您可以编写如下的 return语句:

In C++ you are allowed to write a return statement that looks like :

return ( ... );

与流行的不同:

return ... ;

特别是,第一个版本返回包含该return语句的函数堆栈本地内容的地址/引用.

In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement.

现在为什么有些人想返回对当时没有生命期的东西的引用?

Now why something would like to return a reference to something that, at that point, has no lifetime ?

该习语的用例是什么?考虑到C ++ 11和C ++ 14中新的流行词和功能,对此有不同的用法吗?

What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C++14 there is a different usage for this ?

推荐答案

Pre C ++ 1y 返回的带括号的版本是相同的,如果我们看一下 C ++ 11标准草案部分6.6 跳转语句返回的语法为:

Pre C++1y the parenthesized version of the return is identical, if we look at the C++11 draft standard section 6.6 Jump statements, the grammar for return is:

返回表达式 opt ;

返回braced-init-list;

return braced-init-list ;

一个 expression 可以是任何 expression ,我们可以从5.1 主表达式部分中看到( ):

an expression can be any expression and we can see from section 5.1 Primary expressions says (emphasis mine going forward):

带括号的表达式是主表达式,其类型和 值与所附表达式相同.在场 括号不会影响表达式是否为左值. 带括号的表达式可以在完全相同的上下文中使用 与那些可以使用封闭表达式的表达式相同 含义,除非另有说明.

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 (自动)推断返回类型,这会改变情况,正如我们从草稿的C ++ 1y标准部分7.1.6.4 自动说明符说:

In C++1y we can use delctype(auto) to deduce return types and this changes the situation as we can see from the draft C++1y standard section 7.1.6.4 auto specifier says:

初始化使用占位符类型声明的变量时,或者 return语句出现在以以下类型的返回类型声明的函数中: 包含占位符类型,推导的返回类型或变量类型 由其初始值设定项的类型确定.[...]

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.[...]

并包含以下示例:

自动x3a = i;//decltype(x3a)为int

auto x3a = i; // decltype(x3a) is int

decltype(auto)x3d = i;//decltype(x3d)为int

自动x4a =(i);//decltype(x4a)为int

auto x4a = (i); // decltype(x4a) is int

decltype(auto)x4d =(i);//decltype(x4d)为int&

,我们可以看到使用 delctype(auto)和带括号的表达式会有区别.正在应用的规则来自7.1.6.2 简单类型说明符段落 4 ,其中指出:

and we can see there is a difference when using delctype(auto) and parenthesized expressions. The rule that is being applied is from section 7.1.6.2 Simple type specifiers paragraph 4 which says:

对于表达式e,由decltype(e)表示的类型定义如下:

For an expression e, the type denoted by decltype(e) is defined as follows:

并包括以下项目符号:

-如果e是未括号的id表达式或未括号的类 成员访问权限(5.2.5),decltype(e)是由命名的实体的类型 e.如果没有这样的实体,或者e命名一组重载 功能,程序格式不正确;

— 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;

和:

-否则,如果e为左值,则decltype(e)为T& ;,其中T为类型 的e;

— otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;

这篇关于C ++ 11/14和return(...)vs return的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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