2重载具有类似的转换 [英] 2 overloads have similar conversions

查看:253
本文介绍了2重载具有类似的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此相似的问题
http://stackoverflow.com/questions/ 1950840 / c-function-overloading-similar-conversions
已被问及我理解问题的一般前提。寻找解决方案。



我有两个重载函数:

 虚拟IDataStoreNode * OpenNode(const char * Name,bool bCreateIfNotExist,int debug = 0){return 0; 
}

virtual IDataStoreNode * OpenNode(const char * Name,int debug = 0)const {return 0; }

从错误中可以看出,bool和int不能用于区分函数重载。 p>

问题是,有办法解决这个问题吗?

解决方案

bool int 可以用于区分函数重载。正如所料, bool 参数将优先于 bool 重载和 int arguments - int 重载。



根据错误信息判断(我假设你的问题的标题是你所提供的参数不是 bool 也不是 int ,但转换为 bool int 存在且具有相同的排名。



例如,考虑这个

  void foo(bool); 
void foo(int);

int main(){
foo(0); // OK
foo(false); // OK
foo(0u); //错误:不明确
}

前两个调用将成功解决, 。第三个调用不会解析,因为参数类型实际上是 unsigned int ,然而它支持隐式转换 bool int ,因此使通话模糊。



向我们显示您尝试传递的参数。


A similar question to this http://stackoverflow.com/questions/1950840/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.

这篇关于2重载具有类似的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆