Overloading a function that returns a vector so that it can return a vector of different type [duplicate]

StackOverflow https://stackoverflow.com/questions/20873870

Question

I have had this problem at numerous times in my code (and now, I think there is no circumventing it): For some reason, when I try to write a method that returns a std::vector<long double> and try to overload it with the same method name that returns a different std::vector, say, std::vector<std::complex<long double> >, I get an error message akin to : std::vector<std::vector<long double> > cannot be overloaded with std::vector<long double> even though I the necessary classes #include'd. Why is this?? Is there any rationale behind this??

Here is some code simulating the problem:

#ifndef MATRIXALGORITHMS_H
#define MATRIXALGORITHMS_H
#include <complex>
#include <vector>

class MatrixAlgorithms
{
    public:
        MatrixAlgorithms();
        //two algorithms that are not strictly for matrices; they are for solving quadratic and cubic polynomials
        //quadratic method; the roots might be real, so there should be two versions of this algorithm
        std::vector<long double> quadraticFormula(long double, long double, long double);
        std::vector<std::complex<long double> > quadraticFormula(long double, long double, long double);
    protected:
    private:
};

#endif // MATRIXALGORITHMS_H

I tried to compile it and it gave me the aforementioned error....

Was it helpful?

Solution

Return type of a method is not used in an overload resolution.

Why ? Many reasons, but the simplest is because you wouldn't be able to guess what function is called when the returned value is ignored, which is perfectly legal.

E.g.:

int f();
double f();


f(); // Which one would you call ?

Other more complicated reasons can be easily found:

int f();
double f();

void g(int);
void g(double);

g(f()); // Which f and g do you call ?

Not to mention auto and so on...

OTHER TIPS

Return type does not take part in overload resolution.

The return type is not part of a function's signature. It doesn't really make much sense. How do you expect type deduction to work when only the return type changes? i.e.:

auto func = ambiguous(0.0, 0.0, 0.0); // Which one to use?

You can specify an out parameter. This is a common idiom in Microsoft:

quadraticFormula(long double, long double, long double, TypeOne& out);

Although this will require your users to rely on Intellisense or documentation. The other option is to rename your functions.

quadraticFormulaOne(...);
quadraticFormulaTwo(...);

But this can be annoying if there's a lot of them.


C++ (subsection 7.4.1 of Bjarne Stroustrup's "The C++ Programming Language"): "Return types are not considered in overload resolution. The reason is to keep resolution for an individual operator or function call context-independent. Consider:

float sqrt(float);
double sqrt(double);

void f(double da, float fla)
{
    float fl = sqrt(da);     // call sqrt(double)
    double d = sqrt(da); // call sqrt(double)
    fl = sqrt(fla);            // call sqrt(float)
    d = sqrt(fla);             // call sqrt(float)
}

If the return type were taken into account, it would no longer be possible to look at a call of sqrt() in isolation and determine which function was called."

See Function overloading by return type? for an extensive discussion.

This has nothing to do with the vector; you can't overload on return type.

[C++11: 13.1/2]: Certain function declarations cannot be overloaded:

  • Function declarations that differ only in the return type cannot be overloaded.
  • [..]

As for the rationale: well, how would the compiler know which function you meant to call? It can only choose based on the arguments you give it.

Your expectation is rather akin to walking into a restaurant, and initiating the following interaction:

<mike_warren> Good evening. I would like my favourite meal, please.
<waiter> What is your favourite meal, sir?
<mike_warren> That information is not available to you.
<waiter> Could you provide any hints or tips that may enable me to determine which food you would like served?
<mike_warren> No, sorry. You will only find out what my favourite food is after you have served it.
<waiter> Sir, that doesn't make a lot of sense. I cannot serve your favourite food without knowing in advance what it is… only from that information will the chef know how to prepare your meal.
<mike_warren> Then I think we have a problem.
<waiter> Leave before I call the police.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top