c++重载运算符解析

发布于 2024-11-25 00:49:05 字数 1206 浏览 1 评论 0原文

我正在学习 C++ 并使用 C++ Primer。考虑以下练习 14.46:

 class Complex {
     Complex(double);
     // ...
 };

 class LongDouble {

     friend LongDouble operator+(LongDouble&, int);  // (1)

 public:
     LongDouble(int);

     operator double();

     LongDouble operator+(const Complex &);  // (2)
     // ...
  };

 LongDouble operator+(const LongDouble &, double);  // (3)

 LongDouble ld(16.08);
 double res = ld + 15.05; // which operator+ ?

当我使用 gcc 4.5 编译上述程序时,我得到

14_46.cpp:60:21: error: ambiguous overload for ‘operator+’ in ‘ld + 1.5050000000000000710542735760100185871124267578125e+1’
14_46.cpp:60:21: note: candidates are: operator+(double, double) <built-in>
14_46.cpp:35:5: note:                 LongDouble LongDouble::operator+(const Complex&)
14_46.cpp:45:1: note:                 LongDouble operator+(const LongDouble&, double)
14_46.cpp:17:5: note:                 LongDouble operator+(LongDouble&, int)

Why is (3) not selected ?不是完全匹配吗?

但是,我注意到删除 (3) 中参数的 const-ness 完全匹配,即

LongDouble operator+(LongDouble &, double);  // (4)

使用 (4) 没有歧义。我在这里错过了什么吗?

I'm learning c++ and using C++ Primer. Consider the following exercise 14.46:

 class Complex {
     Complex(double);
     // ...
 };

 class LongDouble {

     friend LongDouble operator+(LongDouble&, int);  // (1)

 public:
     LongDouble(int);

     operator double();

     LongDouble operator+(const Complex &);  // (2)
     // ...
  };

 LongDouble operator+(const LongDouble &, double);  // (3)

 LongDouble ld(16.08);
 double res = ld + 15.05; // which operator+ ?

When I compile using gcc 4.5 the above program, I get

14_46.cpp:60:21: error: ambiguous overload for ‘operator+’ in ‘ld + 1.5050000000000000710542735760100185871124267578125e+1’
14_46.cpp:60:21: note: candidates are: operator+(double, double) <built-in>
14_46.cpp:35:5: note:                 LongDouble LongDouble::operator+(const Complex&)
14_46.cpp:45:1: note:                 LongDouble operator+(const LongDouble&, double)
14_46.cpp:17:5: note:                 LongDouble operator+(LongDouble&, int)

Why is (3) not selected ? Isn't it exact match?

However, I noticed that removing const-ness of parameter in (3) matches exactly, i.e.,

LongDouble operator+(LongDouble &, double);  // (4)

Using (4) there is no ambiguity. Am I missing something here?

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

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

发布评论

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

评论(2

眸中客 2024-12-02 00:49:05

您有以下竞争的用户定义函数(候选)

operator+(LongDouble&, int); // friend
operator+(LongDouble&, Complex const&); // member
operator+(LongDouble const&, double); // global

您正在使用参数调用此函数:

(LongDouble&, double)

对于第一个参数,前两个候选函数比最后一个更好。对于第二个参数,最后一个候选者比前两个候选者更好。没有一个候选人在所有论点上都至少与其他所有候选人一样好,并且在某些论点上有更好的匹配。

在这种情况下,你无法找出明显的赢家。这就是我喜欢说的“十字”。

在用户定义的候选者中也考虑了内置候选者。

operator+(double, double);
operator+(int, double);
...

从所有内置候选者中,operator+(double, double) 匹配得最好。但这需要对第一个参数进行用户定义的转换,这比第一个参数的所有其他三个用户定义的运算符更糟糕,因此它也无法获胜。

You have the following competing user defined functions (candidates)

operator+(LongDouble&, int); // friend
operator+(LongDouble&, Complex const&); // member
operator+(LongDouble const&, double); // global

You are invoking this with arguments:

(LongDouble&, double)

For the first argument, the first two candidates are better than the last. For the second argument, the last candidate is better than the first two candidates. No candidate has at least as good matches as all the others for all arguments, and better matches for some arguments.

You cannot figure out a clear winner for this situation. This is what I like to call the "criss cross".

There are builtin candidates too that are considered among user defined candidates.

operator+(double, double);
operator+(int, double);
...

From all the builtin candidates, operator+(double, double) would match best. But that would require a user defined conversion for the first argument, which is worse than all the three other user defined operators for the first argument, so it cannot win either.

谁人与我共长歌 2024-12-02 00:49:05

我首选的解决方法是将 const 添加到版本 1:

friend LongDouble operator+(const LongDouble&, int);  // (1)

My preferred way to fix this would be to add const to version 1:

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