Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are parentheses around the result significant in a return statement?

Tags:

c++

c

semantics

Is there a difference between these two statements inside a function?

bool returnValue = true; // Code that does something return(returnValue); 

and this?

bool returnValue = true; // Code return returnValue; 

The former has parentheses around returnValue.

like image 371
Jose Villalta Avatar asked Jan 21 '11 18:01

Jose Villalta


People also ask

Do return statements need parentheses?

So basically return is a statement with an optional expression list as an argument. Therefore parentheses are not required and only preferrable when necesary (i.e. for breaking precedences).

What are the significances of return statement?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

Does return need parentheses Javascript?

It is not required. It's just a choice. It might also being used to avoid automatic semicolon insertion.

Which of the following is true about function return statements?

Which of the following is true about function return statements? A function can hold multiple return statements, but only one return statement executes in one function call.


2 Answers

As of C++14, they often are.

C++14 adds a fringe case where parentheses around a return value may alter the semantics. This code snippet shows two functions being declared. The only difference is parentheses around the return value.

int var1 = 42; decltype(auto) func1() { return var1; } // return type is int, same as decltype(var1) decltype(auto) func1() { return(var1); } // return type is int&, same as decltype((var1)) 

In the first func1 returns an int and in the second one func1 returns an int& . The difference in semantics is directly related to the surrounding parentheses.

The auto specifier in its latest form was introduced in C++11. In the C++ Language Spec it is described as:

Specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type or will be deduced from its return statements (since C++14)

As well C++11 introduced the decltype specifier which is described in the C++ Language Spec:

Inspects the declared type of an entity or queries the return type of an expression.

[snip]

  1. If the argument is either the unparenthesised name of an object/function, or is a member access expression (object.member or pointer->member), then the decltype specifies the declared type of the entity specified by this expression.

  2. If the argument is any other expression of type T, then

    a) if the value category of expression is xvalue, then the decltype specifies T&&

    b) if the value category of expression is lvalue, then the decltype specifies T&

    c) otherwise, decltype specifies T

[snip]

Note that if the name of an object is parenthesised, it becomes an lvalue expression, thus decltype(arg) and decltype((arg)) are often different types.

In C++14 the ability to use decltype(auto) was allowed for function return types. The original examples are where the semantic difference with parentheses comes into play. Revisiting the original examples:

int var1 = 42; decltype(auto) func1() { return var1; } // return type is int, same as decltype(var1) decltype(auto) func1() { return(var1); } // return type is int&, same as decltype((var1)) 

decltype(auto) allows the trailing return type in the function to be deduced from the entity/expression on the return statement. In the first version return var1; is effectively the same as returning the type decltype(var1) (an int return type by rule 1 above) and in the second case return (var1); it's effectively the same as decltype((var1)) (an int & return type by rule 2b).

The parentheses make the return type int& instead of int, thus a change in semantics. Moral of the story - "Not all parentheses on a return type are created equal"

like image 187
Michael Petch Avatar answered Oct 14 '22 18:10

Michael Petch


There is no difference.

One reason to use parenthesis would be if you wanted to evaluate an expression before returning but in your example, there would be no reason. See:

Parenthesis surrounding return values

for further discussion.

like image 44
Karl Rosaen Avatar answered Oct 14 '22 18:10

Karl Rosaen