C ++ 11/14 and return (...) vs return

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

return ( ... ); 

which differs from the more popular:

 return ... ; 

In particular, the first version returns the address / link of what is local to the function stack that contains this return .

Now for some reason I would like to return a link to something that at this moment does not have life?

What is the precedent for this idiom? Given the new buzzword and features from C ++ 11 and C ++ 14, is there another use for this?

+3
c ++ c ++ 11 c ++ 14 decltype type-deduction
Jun 09 '14 at 13:50
source share
3 answers

Pre C ++ 1y the copied version of the return is identical if we look at the C ++ 11 draft 6.6 Transition operators, the grammar for the return:

return expression opt ;

return braced-init-list;

an expression can be any expression, and we can see from section 5.1 . Key expressions say (emphasis mine forward):

The expression in parentheses is the primary expression, the type and value are identical to the values ​​of the attached expression. The presence of parentheses does not affect whether the expression is a value of l. An expression in parentheses can be used in exactly the same contexts as those in which a closed expression can be used, and with the same unless otherwise indicated .

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

When a variable declared using a placeholder type is initialized, or return occurs in a function declared with a return type that contains a placeholder type, the output type of the return value or the type of the variable is determined by the type of its initializer . [...]

and contains the following examples:

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

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

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

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

and we can see that there is a difference when using delctype (auto) expressions and in parentheses. The rule that applies consists of clause 7.1.6.2 specifications of a simple type, which states:

For the expression e, the type designated as decltype (e) is defined as follows:

and includes the following bullets:

- if e is an unsferonized id-expression or an unbiased membership access class (5.2.5), decltype (e) is the type of object called e. If such an object is absent or if e calls a set of overloaded functions, the program is poorly formed;

and

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

+7
Jun 09 '14 at 14:28
source share

Form return expression;
An expression can be anything, including an expression in parentheses.
These are not different forms of returns, however different types will be output along with decltype(auto) .

+12
Jun 09 '14 at 13:53 on
source share

The two versions differ in context when automatic return type inference with decltype(auto) used in C ++ 14

In particular, the second is antipater in case B (an example taken from frequently asked questions on C ++ )

 decltype(auto) look_up_a_string_1() { auto str = lookup1(); return str; } //A decltype(auto) look_up_a_string_2() { auto str = lookup1(); return(str); } //B 

since it returns string& (as opposed to string to A), which is a reference to the str local variable.

+10
Jun 09 '14 at
source share



All Articles