Are parentheses around the result significant in a return statement?

C++CSemantics

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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][1] 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][2]:

>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"

[1]: http://en.cppreference.com/w/cpp/language/auto "C++ Language Spec" [2]: http://en.cppreference.com/w/cpp/language/decltype "C++ Language Spec"

Solution 2 - C++

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:

https://stackoverflow.com/questions/161879/parenthesis-surrounding-return-values

for further discussion.

Solution 3 - C++

The parenthesis on the upper example are superfluous; they are effectively ignored.

It would be the same as something like...

int x = (5);

The parenthesis here are ignored as well.

Solution 4 - C++

AFAIK, nothing is different.

In C++, expressions can have the form: expr or (expr). So, the latter is an expression with more typing. For further reading about this, refer to a grammar (look for "expression").

Solution 5 - C++

No, there are no difference in your code.

Solution 6 - C++

I assume boo is a typo, and you are asking whether there is a difference between

return expr;

and

return(expr);

The answer is no.

Solution 7 - C++

No difference!!

People use parenthesis if there's a complex expression involved.

BTW return is a statement not a function.

Solution 8 - C++

Nope there's no difference between the two, although you can include parenthesis if it makes the expression easy to read and clear.

Solution 9 - C++

You're abusively slowing down the compiler!

The presence of parenthesis not only slow down the preprocessing phase, but they generate a more complicated Abstract Syntax Tree too: more memory, more computation.


From a semantic point of view ? They are exactly identical. Whether there are parenthesis or not the return statement will fully evaluate the expression before returning it.

Solution 10 - C++

They are identical. I see the parenthesis syntax quite often, and I always ask those who use it: why? And none can answer why they use it.

To bluntly sum it up, parenthesis around returning expressions are used by people who don't quite grasp the difference between function-like macros and functions, or who are confused about the operator precedence or order of evaluation rules in C. There is no coding style benefit from using parenthesis.

Thus

return value;

is more correct than

return (value)

because the latter suggests you don't quite know what you are doing :)

Solution 11 - C++

Both are the same in your case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJose VillaltaView Question on Stackoverflow
Solution 1 - C++Michael PetchView Answer on Stackoverflow
Solution 2 - C++Karl RosaenView Answer on Stackoverflow
Solution 3 - C++JamesView Answer on Stackoverflow
Solution 4 - C++David WeiserView Answer on Stackoverflow
Solution 5 - C++ElalferView Answer on Stackoverflow
Solution 6 - C++ascheplerView Answer on Stackoverflow
Solution 7 - C++Prasoon SauravView Answer on Stackoverflow
Solution 8 - C++sanchit.h View Answer on Stackoverflow
Solution 9 - C++Matthieu M.View Answer on Stackoverflow
Solution 10 - C++LundinView Answer on Stackoverflow
Solution 11 - C++Zeshan KhanView Answer on Stackoverflow