C++加法过载歧义

发布于 2024-08-28 05:10:57 字数 1688 浏览 7 评论 0原文

我的代码库中遇到了一个令人烦恼的难题。我不太清楚为什么我的代码会生成此错误,但(例如) std::string 不会。

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

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

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

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 中生成以下错误:

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) 构造函数作为隐式构造函数非常方便,使用显式关键字来解决这个问题只会导致一堆不同的问题。

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

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 构造函数未显式声明。这如何不会导致我遇到的相同错误,以及如何实现相同的行为?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£冰雨忧蓝° 2024-09-04 05:10:57

造成歧义的原因是,只有当一个候选函数的参数没有一个比另一个候选函数的参数匹配更差时,该候选函数才比另一个候选函数更好。考虑您的两个函数:

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

您正在使用 Stringconst char* 调用 operator+

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

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

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

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

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

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

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)

You are calling operator+ with a String and a const char*.

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

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

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:

  • 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

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

夏雨凉 2024-09-04 05:10:57

在这种情况下,只需在 operator+ 上定义就足够了:

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

因为您提供了一个采用 char* 的构造函数,所以可以从调用 operator+ 期间的 char*。例如:

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

String helloWorld = hello + world;

将使用 char* world 的内容构造一个临时 String (因为您的构造函数不显式),然后两个String 对象将被传递给operator+

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+.

七七 2024-09-04 05:10:57

如果您按照应有的方式声明成员 + 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);

不要告诉我你的字符串有隐式转换为 const char*...这很邪恶。

The error goes away if you declare the member + const as it should be.

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

Not sure what's the reason, though. Perhaps it prefers binding arguments to const reference if possible, so the first overload is a better match for the left-hand value and the third overload has a better match for the right-hand value.

Better explanation. (Must have misread the problem a bit.)


printf(result);

Don't tell me your String has implicit conversion to const char*... That's evil.

可是我不能没有你 2024-09-04 05:10:57

模板函数和非模板函数遵循不同的规则。模板函数是根据实际参数类型选择的,不应用任何转换。在非模板(即您的代码)的情况下,可以应用隐式转换。因此 basic_string 中的模板化内容并不含糊,但你的却是。

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.

世俗缘 2024-09-04 05:10:57

您已经展示了 basic_string 具有与 String 类中的第二个和第三个运算符相对应的 operator+ 实现。 basic_string 是否也有一个与您的第一个运算符 [friend String operator+ ( const String& lval, const char *rval );] 相对应的运算符?

如果删除这个运算符会发生什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文