A similar question to this C++ Function Overloading Similar Conversions has been asked and i understand the general premise of the problem. Looking for a solution.
I have 2 overloaded functions:
virtual IDataStoreNode* OpenNode(const char *Name, bool bCreateIfNotExist,int debug=0) { return 0;
}
virtual IDataStoreNode* OpenNode(const char* Name,int debug=0) const { return 0; }
From the errors it would appear that bool and int cannot be used to distinguish function overloads.
The question is , is there a way to work around this?
bool
and int
can be used to distinguish function overloads. As one would expect, bool
arguments will prefer bool
overloads and int
arguments - int
overloads.
Judging by the error message (I assume that the title of your question is a part of the error message you got), what you are dealing with is the situation when the argument you supply is neither bool
nor int
, yet conversions to bool
and int
exist and have the same rank.
For example, consider this
void foo(bool);
void foo(int);
int main() {
foo(0); // OK
foo(false); // OK
foo(0u); // ERROR: ambiguous
}
The first two calls will resolve successfully and in expected manner. The third call will not resolve, because the argument type is actually unsigned int
, which however supports implicit conversions to both bool
and int
thus making the call ambiguous.
How are you calling your functions? Show us the arguments you are trying to pass.
For the following functions:
virtual IDataStoreNode* OpenNode(const char *Name, bool bCreateIfNotExist,int debug=0) { return 0; }
virtual IDataStoreNode* OpenNode(const char* Name, int debug=0) const { return 0; }
The following call (as an example, there might be others) would be ambiguous:
unsigned int val = 0; //could be double, float
OpenNode("", val);
Since a unsigned int
can be converted to both bool
and int
, there is ambiguity. The easiest way to resolve it is to cast the parameter to the type of the parameter in your preferred overload:
OpenNode("", (bool)val);
OR
OpenNode("", (int)val);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With