Multiple Return Values โ€‹โ€‹in a Function

I came across the following example:

#include <stdio.h>

// test multiple return                                                                                                                                                             
int foo()
{
    return 1,2,3,4,5,6;
}

// main entry point                                                                                                                                                                 
int main(int argc, char* argv[])
{
    printf("foo returns: %d\n", foo());
    return 0;
}

compile it, then run:

gcc main.cpp -o main
./main

The results confuse me:

foo returns: 6

Question: why there is no compile-time error?

+5
source share
3 answers

Since you are using the operator comma : expression a,bwhere aand bare arbitrary (usually a side effect) are to: evaluate the left side aand discard the result (so aonly evaluated for side effects), then evaluate band give him the result.

You cannot return several things from function C. You must return, for example. a collection (usually a struct) or a pointer with dynamic heap allocation.

, ? . gcc -Wall ( C) g++ -Wall ( ++), :

 egor7.c: In function โ€˜fooโ€™:
 egor7.c:6:13: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:15: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:17: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:19: warning: left-hand operand of comma expression has no effect [-Wunused-value]
 egor7.c:6:21: warning: left-hand operand of comma expression has no effect [-Wunused-value]
+10

:

return 1,2,3,4,5,6;

. ( ) .

6. , , .. . ( 1,2,3,4,5 .)

C ++ . .

+11

, Clang:

$ clang++ -Weverything test.cpp
test.cpp:4:5: warning: no previous prototype for function 'foo' [-Wmissing-prototypes]
int foo()
    ^
test.cpp:6:10: warning: expression result unused [-Wunused-value]
  return 1,2,3,4,5,6;
         ^
test.cpp:6:12: warning: expression result unused [-Wunused-value]
  return 1,2,3,4,5,6;
           ^
test.cpp:6:14: warning: expression result unused [-Wunused-value]
  return 1,2,3,4,5,6;
             ^
test.cpp:6:16: warning: expression result unused [-Wunused-value]
  return 1,2,3,4,5,6;
               ^
test.cpp:6:18: warning: expression result unused [-Wunused-value]
  return 1,2,3,4,5,6;
                 ^
6 warnings generated.

, -Weverything . , , .

For explanations: see The Mystical Answer for a comma and its sequencing effects. One of the useful cases of this operator is:

std::list<Item> list = /**/;
assert(list.size() >= 10);

auto it = list.begin();
for (int i = 0; i < 10; ++i, ++it) {
  std::cout << "Item " << i << ": " << *it << "\n";
}

Note that the third section of the loop foruses the comma operator to perform two operations in the same expression.

Of course, this syntax is mostly anecdotal, so people are regularly surprised ...

+4
source

All Articles