Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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);
OpenNode("", (int)val);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.