C ++加载过载模糊性 [英] C++ addition overload ambiguity

查看:298
本文介绍了C ++加载过载模糊性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码库中遇到了一个令人烦恼的难题。我不知道为什么我的代码生成这个错误,但(例如)std :: string不。

I am coming up against a vexing conundrum in my code base. I can't quite tell why my code generates this error, but (for example) std::string does not.

class String {
public:
    String(const char*str);
    friend String operator+ ( const String& lval, const char *rval );
    friend String operator+ ( const char *lval, const String& rval );
    String operator+ ( const String& rval );
};

这些的实现很容易想象自己。

The implementation of these is easy enough to imagine on your own.

我的驱动程序包含以下内容:

My driver program contains the following:

String result, lval("left side "), rval("of string");
char lv[] = "right side ", rv[] = "of string";
result = lv + rval;
printf(result);
result = (lval + rv);
printf(result);

这会在gcc 4.1.2中生成以下错误:

Which generates the following error in gcc 4.1.2:

driver.cpp:25: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
String.h:22: note: candidate 1: String operator+(const String&, const char*)
String.h:24: note: candidate 2: String String::operator+(const String&)

到目前为止这么好,对吧?可悲的是,我的String(const char * str)构造函数很容易作为一个隐式构造函数,使用显式关键字来解决这个问题只会导致不同的问题。

So far so good, right? Sadly, my String(const char *str) constructor is so handy to have as an implicit constructor, that using the explicit keyword to solve this would just cause a different pile of problems.

此外... std :: string不必诉诸于此,我不知道为什么。例如,在basic_string.h中,它们被声明如下:

Moreover... std::string doesn't have to resort to this, and I can't figure out why. For example, in basic_string.h, they are declared as follows:

template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT, _Traits, _Alloc>
operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
          const basic_string<_CharT, _Traits, _Alloc>& __rhs)

template<typename _CharT, typename _Traits, typename _Alloc>
basic_string<_CharT,_Traits,_Alloc>
operator+(const _CharT* __lhs,
          const basic_string<_CharT,_Traits,_Alloc>& __rhs);

等。 basic_string构造函数未声明为显式。这不会导致我得到相同的错误,我如何实现相同的行为

and so on. The basic_string constructor is not declared explicit. How does this not cause the same error I'm getting, and how can I achieve the same behavior??

推荐答案

因为模糊性是一个候选函数比另一个候选函数优于其参数的 none 比另一个的参数更差的匹配。考虑你的两个函数:

The reason for the ambiguity is that one candidate function is better than another candidate function only if none of its parameters are a worse match than the parameters of the other. Consider your two functions:

friend String operator+(const String&, const char*); // (a)
String operator+(const String&);                     // (b)

您正在调用 operator + String const char *

const char * 类型的第二个参数明显匹配(a)比(b)更好。它是(a)的完全匹配,但是(b)需要用户定义的转换。

The second argument, of type const char*, clearly matches (a) better than (b). It is an exact match for (a), but a user-defined conversion is required for (b).

因此,为了有歧义,第一个参数必须匹配(b)比(a)更好。

Therefore, in order for there to be an ambiguity, the first argument must match (b) better than (a).

左侧的 String operator + 的调用不是常量。因此,它匹配(b),它是一个非const成员函数,比(a)更好,它接受一个 const String&

The String on the left-hand side of the call to operator+ is not const. Therefore, it matches (b), which is a non-const member function, better than (a), which takes a const String&.

因此,以下任何解决方案都会消除歧义:

Therefore, any of the following solutions would remove the ambiguity:


  • 更改成员 operator $ 作为const成员函数

  • 更改非成员运算符+ c $ c> String& 而不是 const String&


  • Change the member operator+ to be a const member function
  • Change the non-member operator+ to take a String& instead of a const String&
  • Call operator+ with a const String on the left hand side

显然,第一个 http://stackoverflow.com/questions/2613645/c-addition-overload-ambiguity/2613836#2613836\"> UncleBens推荐的,是最好的去处。

Obviously, the first, also suggested by UncleBens, is the best way to go.

这篇关于C ++加载过载模糊性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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