2
4

3 回答 3

4

According to the C++ standard, 13.2.1

Two function declarations of the same name refer to the same function if they are in the same scope and have equivalent parameter declarations.

This means that only the name and parameter types are considered; return type is not.

This makes sense, because you can invoke a function with a return value, and disregard its return value. If language designers allowed overloads on the return type, compilers would not be able to resolve these overloads in some legitimate contexts.

于 2013-01-04T02:12:10.720 回答
2

You can't overload by return type because:

int main() {
   f();  // call to void f
   f();  // call to int returning one
   f();  // call to void
   return 0;
}

Are all ambiguous.

float f() { return 0.0f; }
char  f() { return 'a';  }
int   i = f();

is also ambiguous.

于 2013-01-04T02:10:48.210 回答
1

You can't overload by return type; only by parameters.

于 2013-01-04T02:08:04.887 回答