2

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);

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&)

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.

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);

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??

4

5 回答 5

9

模棱两可的原因是,一个候选函数比另一个候选函数更好,只有当它的参数没有一个比另一个参数更差时。考虑您的两个功能:

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

operator+用 aString和 a打电话const char*

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

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

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

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

  • 将成员更改operator+为 const 成员函数
  • 将非成员更改operator+为采取 aString&而不是 aconst String&
  • operator+在左侧使用 const String调用

显然,第一个也是 UncleBens 建议的,是最好的方法。

于 2010-04-10T16:35:58.073 回答
5

It is sufficient in this case just to define on operator+:

String operator+(const String& lval, const String& rval);

Because you provide a constructor taking a char*, a String can be constructed from a char* during the call to operator+. For example:

String hello = "Hello, ";
const char* world = "world!";

String helloWorld = hello + world;

A temporary String will be constructed with the contents of the char* world (because your constructor is not explicit), then the two String objects will be passed to operator+.

于 2010-04-10T14:12:17.747 回答
3

如果您按应有的方式声明成员 + const ,错误就会消失。

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 ) const; //<-- here
};

不过不知道是什么原因。如果可能,它可能更喜欢将参数绑定到 const 引用,因此第一个重载更适合左侧值,而第三个重载更适合右侧值。

更好的解释。(一定是误读了这个问题。)


printf(result);

不要告诉我您的 String 已隐式转换为const char*... 那是邪恶的。

于 2010-04-10T15:13:41.440 回答
2

Template and non-template functions follow different rules. The template functions are selected on the actual parameter types, without any conversions being applied. In the case of the non-template (i.e. your code) an implicit conversion can be applied. Thus the templated stuff in basic_string is not ambiguous, but yours is.

于 2010-04-10T14:58:54.380 回答
1

You've shown that basic_string has implementations of operator+ corresponding to the second and third operators in your class String. Does basic_string also have an operator corresponding to your first operator [friend String operator+ ( const String& lval, const char *rval );]?

What happens if you remove this operator?

于 2010-04-10T14:13:27.853 回答