2 overloads have similar conversions

A similar question to this is Overloading C ++ functions with similar conversions and I understand the general premise of the problem. Search 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 will seem that bool and int cannot be used to distinguish between functional overloads.

The question is, is there a way around this?

+6
c ++
source share
2 answers

bool and int can be used to distinguish between functional overloads. As you would expect, bool arguments will prefer bool overloads and int arguments will int .

Judging by the error message (I assume that the title of your question is part of the error message you received), what you mean is a situation where the argument you provide is neither bool nor int , but the bool and int transformations 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 be successful and expected. The third call will not be allowed, as the type of the argument is actually unsigned int , which, however, supports implicit conversions for both bool and int , which makes the call ambiguous.

What do you call your functions? Show us the arguments you are trying to convey.

+10
source share

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 (for example, there may 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 this is to specify the parameter in the parameter type in the preferred overload:

 OpenNode("", (bool)val); 

OR

 OpenNode("", (int)val); 
+2
source share

All Articles