Overload a C++ function according to the return value

25,356

Solution 1

class mul
{
public:
    mul(int p1, int p2)
    {
        param1 = p1;
        param2 = p2;
    }
    operator int ()
    {
        return param1 * param2;
    }

    operator std::string ()
    {
        return std::string(param2, param1 + '0');
    }

private:
    int param1;
    int param2;
};

Not that I would use that.

Solution 2

You have to tell the compiler which version to use. In C++, you can do it three ways.

Explicitly differentiate the calls by typing

You somewhat cheated because you sent an integer to a function waiting for a char, and wrongly sent the number six when the char value of '6' is not 6 but 54 (in ASCII):

std::string mul(char c, int n) { return std::string(n, c); }

std::string s = mul(6, 3); // s = "666"

The right solution would be, of course,

std::string s = mul(static_cast<char>(54), 3); // s = "666"

This was worth mentioning, I guess, even if you did not want the solution.

Explicitly differentiate the calls by dummy pointer

You can add a dummy parameter to each functions, thus forcing the compiler to choose the right functions. The easiest way is to send a NULL dummy pointer of the type desired for the return:

int mul(int *, int i, int j) { return i*j; }
std::string mul(std::string *, char c, int n) { return std::string(n, c); }

Which can be used with the code:

int n = mul((int *) NULL, 6, 3); // n = 18
std::string s = mul((std::string *) NULL, 54, 3); // s = "666"

Explicitly differentiate the calls by templating the return value

With this solution, we create a "dummy" function with code that won't compile if instantiated:

template<typename T>
T mul(int i, int j)
{
   // If you get a compile error, it's because you did not use
   // one of the authorized template specializations
   const int k = 25 ; k = 36 ;
}

You'll note this function won't compile, which is a good thing because we want only to use some limited functions through template specialization:

template<>
int mul<int>(int i, int j)
{
   return i * j ;
}

template<>
std::string mul<std::string>(int i, int j)
{
   return std::string(j, static_cast<char>(i)) ;
}

Thus, the following code will compile:

int n = mul<int>(6, 3); // n = 18
std::string s = mul<std::string>(54, 3); // s = "666"

But this one won't:

short n2 = mul<short>(6, 3); // error: assignment of read-only variable ‘k’

Explicitly differentiate the calls by templating the return value, 2

Hey, you cheated, too!

Right, I did use the same parameters for the two "overloaded" functions. But you did start the cheating (see above)...

^_^

More seriously, if you need to have different parameters, then you will to write more code, and then have to explicitly use the right types when calling the functions to avoid ambiguities:

// For "int, int" calls
template<typename T>
T mul(int i, int j)
{
   // If you get a compile error, it's because you did not use
   // one of the authorized template specializations
   const int k = 25 ; k = 36 ;
}

template<>
int mul<int>(int i, int j)
{
   return i * j ;
}

// For "char, int" calls
template<typename T>
T mul(char i, int j)
{
   // If you get a compile error, it's because you did not use
   // one of the authorized template specializations
   const int k = 25 ; k = 36 ;
}

template<>
std::string mul<std::string>(char i, int j)
{
   return std::string(j, (char) i) ;
}

And this code would be used as such:

int n = mul<int>(6, 3); // n = 18
std::string s = mul<std::string>('6', 3); // s = "666"

And the following line:

short n2 = mul<short>(6, 3); // n = 18

Would still not compile.

Conclusion

I love C++...

:-p

Solution 3

If you wanted to make mul be a real function instead of a class, you could just use an intermediate class:

class StringOrInt
{
public:
    StringOrInt(int p1, int p2)
    {
        param1 = p1;
        param2 = p2;
    }
    operator int ()
    {
        return param1 * param2;
    }

    operator std::string ()
    {
        return std::string(param2, param1 + '0');
    }

private:
    int param1;
    int param2;
};

StringOrInt mul(int p1, int p2)
{
    return StringOrInt(p1, p2);
}

This lets you do things like passing mul as a function into std algorithms:

int main(int argc, char* argv[])
{
    vector<int> x;
    x.push_back(3);
    x.push_back(4);
    x.push_back(5);
    x.push_back(6);

    vector<int> intDest(x.size());
    transform(x.begin(), x.end(), intDest.begin(), bind1st(ptr_fun(&mul), 5));
    // print 15 20 25 30
    for (vector<int>::const_iterator i = intDest.begin(); i != intDest.end(); ++i)
        cout << *i << " ";
    cout << endl;

    vector<string> stringDest(x.size());
    transform(x.begin(), x.end(), stringDest.begin(), bind1st(ptr_fun(&mul), 5));
    // print 555 5555 55555 555555
    for (vector<string>::const_iterator i = stringDest.begin(); i != stringDest.end(); ++i)
        cout << *i << " ";
    cout << endl;

    return 0;
}

Solution 4

No.

You can't overload by return value because the caller can do anything (or nothing) with it. Consider:

mul(1, 2);

The return value is just thrown away, so there's no way it could choose an overload based on return value alone.

Solution 5

Use implicit conversion in an in between class.

class BadIdea
{
  public:
    operator string() { return "silly"; }
    operator int() { return 15; }
};

BadIdea mul(int, int)

You get the idea, terrible idea though.

Share:
25,356
Motti
Author by

Motti

тнιѕ αвσυт вσχ ιηтєηтισηαℓℓу ℓєƒт ยภгєค๔ค๒lє

Updated on April 17, 2020

Comments

  • Motti
    Motti about 4 years

    We all know that you can overload a function according to the parameters:

    int mul(int i, int j) { return i*j; }
    std::string mul(char c, int n) { return std::string(n, c); } 
    

    Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used:

    int n = mul(6, 3); // n = 18
    std::string s = mul(6, 3); // s = "666"
    // Note that both invocations take the exact same parameters (same types)
    

    You can assume the first parameter is between 0-9, no need to verify the input or have any error handling.

  • Johannes Schaub - litb
    Johannes Schaub - litb over 14 years
    You can add conversion functions like operator identity<int(*)()>::type() { return &operator int; }, which allows passing it to a function taking a int(*)(), and if the function pointer is called, the operator int function is called.
  • jrodman
    jrodman about 9 years
    I'm torn. On the plus side, it's clever and onpoint. On the minus side it's a loaded gun.
  • paercebal
    paercebal about 9 years
    @jrodman : Could you please elaborate on the "loaded gun" with technical input, please? I don't see how anything I wrote in this answer (dummy pointer, or templated return value) goes against the language.
  • Rusty Horse
    Rusty Horse almost 8 years
    @jrodman: C++ is a loaded gun on it's own. That is why we love it so much :)