Why can't functions be overloaded by return type?

22,416

Solution 1

You can't overload on return types as it is not mandatory to use the return value of the functions in a function call expression.

For example, I can just say

GetVal();

What does the compiler do now?

Solution 2

The return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. Each of the following:

  • Number of arguments
  • Type of arguments
  • Sequence of arguments

are the parameters which are used to generate the unique mangled name for each function. It is on the basis of these unique mangled names that compiler can understand which function to call even if the names are same(overloading).

Share:
22,416
nakiya
Author by

nakiya

Feeling lost in the land of gods

Updated on November 23, 2020

Comments

  • nakiya
    nakiya almost 3 years

    Possible Duplicates:
    Function overloading by return type?
    Puzzle: Overload a C++ function according to the return value

    Because I have a library which exposes a bunch of functions in the form of:

    bool GetVal();
    double GetVal();
    int GetVal();
    long GetVal();
    //So on.
    

    And now I have to wrap these. I'd rather not rewrite the same set of functions again. I'd like to do something like

    template<class T>
    T GetVal(){}
    

    But I can't seem to get this working. Any ideas?

  • ephemient
    ephemient almost 13 years
    There's no difference between template<class T> T GetVal() and template<typename T> T GetVal(), and the question says that the first doesn't work. Of course, it would be nice to know why nakiya believes that, but without asking that 1. isn't too useful.
  • Drew Hall
    Drew Hall almost 13 years
    @ephemient: Look again--he's passed a dummy parameter to make overloading work.
  • Mark Ransom
    Mark Ransom almost 13 years
    The compiler could just emit an "Ambiguous return type" message and refuse to compile. This isn't enough justification by itself.
  • Chubsdad
    Chubsdad almost 13 years
    @Mark Ransom: Beg to differ here. Refer $7.4.1 of "The C++ Programming Lanugage 3rd Edition" by Stroustrup
  • Chubsdad
    Chubsdad almost 13 years
    Even if return type was used for name mangling, it would not be possible to determine the exact function to be called if the return value of the function call was discarded.
  • Karl Knechtel
    Karl Knechtel almost 13 years
    Alternatively: suppose we define float GetVal() and double GetVal(), and write int x = GetVal();. Now what? Either a float or a double may be implicitly converted to int.
  • Chubsdad
    Chubsdad almost 13 years
    @Karl Knechtel: No. the compiler would not allow to define such overloads in the first place by giving an error.
  • MSalters
    MSalters almost 13 years
    To be precise: the fact that name mangling doesn't account for the return type is a result, not a cause. If the standard had said that it was possible, name mangling qwould have included the return type.
  • visitor
    visitor almost 13 years
    I think one thing is that the language simply doesn't allow it, the other thing is why not. "What does the compiler do now?" - It could emit an error, which would be a good thing, because it is likely a logical error not to use (or explicitly ignore) the result. As to Karl's "Now what?" - the exact same reasoning could also be applied to overloading on parameters: void f(double); void f(float); f(10); "Now what?". It's not a premise of overloading that it has to be ambiguity-free. - Besides, if you look at the linked question, overloading on return type can be emulated in C++.
  • Alok Save
    Alok Save almost 13 years
    @Chubsdad,MSalters: Agreed, point well made!
  • josesuero
    josesuero almost 13 years
    I guess a more general form of this argument would be "should the result of calling a function depend on where it is called from?" That seems to be a dangerous principle. If I call a function F(x), the result should be the same no matter who I am and where I called it from and what I plan to do with the result.
  • UNSTABLE
    UNSTABLE over 6 years
    @jalf But in this case int F(x) and double F(x) are two different functions. In any case, even calling int F(x) twice is never guaranteed to return the same result due to it storing data internally (via static variables) or returning different results based on other class variables or external data sources it is accessing. A guarantee that a function F(x) will always return the same result is only possible in a few specialty languages that impose a lot of other restrictions in order to make that guarantee, and C++ is not one of them.