I am assuming that the function already has a return value so that cannot be added.
What I came up with to solve this problem is to add extra pointer parameters which default to nullptr.
Before:
bool fun(double a, std::vector<std::randomexample> const & b)
After:
bool fun(double a, std::vector<std::randomexample> const & b, int* extraoutput = nullptr)
and use it like this
if(extraoutput)
*extraoutput = whatever;
But that’s just what I came up with.
I would like to know if there is a better way to do this. Note that “whatever” is already in the function.
If for some reason you need binary as well as (mostly) source compatibility[*]:
Before:
After:
If you don’t want function overloading (for example if
funis part of anextern "C"interface), then you don’t actually have to call the new functionfun. It could just as well befun2.[*] As AndreyT points out, the source compatibility of your solution is limited. Calls to your old function will call your new function fine, but some other things that you might do with the old function will not work fine (since you have changed its type).
There’s actually a source incompatibility in my code too.
void(*foo)() = (void(*)()) fun;is allowed before the overload is added, but afterwards it’s ambiguous. If you want to support code that does that, then that’s a second reason not to want function overloading.