Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 overloads have similar conversions

Tags:

c++

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?

like image 689
Pradyot Avatar asked Sep 16 '10 01:09

Pradyot


2 Answers

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.

like image 108
AnT Avatar answered Sep 19 '22 06:09

AnT


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);
like image 36
Igor Zevaka Avatar answered Sep 23 '22 06:09

Igor Zevaka