skippr
skippr

Reputation: 2706

Return Expressions in C++

Consider the following code:

float validateEntry()
{
    string entry;
    float value;

    getline(cin, entry);
    value = atof(entry.data());

    return ((isNumber(entry) && value >= 0) ? i
    : (cout << "Enter valid amount: ", validateEntry())
}

Why is the last line (the comma-separated expression) allowed, and are there other expressions that can be used with return statements in C++?

I'm mostly confused at the use of the comma, and wondering where this expression syntax is defined (I had no idea it existed, nor would I have known where to find out). Can I fill that last expression with an indefinite amount of code; if so, what are the limitations, requirements, etc.?

Upvotes: 0

Views: 3999

Answers (4)

Mikey
Mikey

Reputation: 11

return [expression];

expression :
   expression , expression
   literal
   etc...

The comma operator separates multiple expressions on a single line. When used in an assignment statement like the return statement (assigning a temporary value which is returned), only the rightmost value is assigned. The preceding expressions are executed from left to right. In your example, the final function call returned value is returned.

I use this technique to clear formal argument variables on failure. Especially COM routines where there is a failure. For example:

HRESULT func(..., IInterface **ppv)
{
...
If(!good)
  return (*ppv = 0), E_FAIL;
...
}

Upvotes: 1

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

? : is the only ternary opeartor of C++

its syntax is logical-or-expression ? expression : conditional-expression

You can read more about it at MSDN

Upvotes: 0

bames53
bames53

Reputation: 88215

, is an operator just like + or << so you can use it freely almost wherever an expression is expected. Exceptions are contexts where a comma is expected with a different meaning, such as in a function call. In such contexts you have to disambiguate the comma by adding an extra set of parens:

foo((b,c)); // call foo with a single argument

The statement you show is the equivalent of:

// return ((isNumber(entry) && value >= 0) ? i : (cout << "Enter valid amount: ", validateEntry())

if(isNumber(entry) && value >= 0) {
    return i;
} else {
    cout << "Enter valid amount: ";
    return validateEntry();
}

It's just a 'clever' way of conditionally executing the cout << "Enter valid amount: " before calling validateEntry() while using as few lines as possible. Of course the whole function is just a 'clever' way to repeatedly ask for an input until it gets valid input. IMO a loop would be better than recursion here.

Upvotes: -1

Josh
Josh

Reputation: 992

The comma operator allows you to group two otherwise-unrelated expressions. Both expressions are always evaluated, and the result is the result of the second expression. It is almost always a bad idea to use it (because it hurts readability just to save a line of code), except maybe in the top of a for-loop.

Upvotes: 1

Related Questions