简体   繁体   English

c++ 重载算子解析

[英]c++ overload operator resolution

I'm learning c++ and using C++ Primer.我正在学习 c++ 并使用 C++ Primer。 Consider the following exercise 14.46:考虑以下练习 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当我使用 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)? Isn't it exact match?不是完全一致吗?

However, I noticed that removing const-ness of parameter in (3) matches exactly, ie,但是,我注意到在 (3) 中删除参数的 const-ness 完全匹配,即

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

Using (4) there is no ambiguity.使用(4)没有歧义。 Am I missing something here?我在这里错过了什么吗?

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:您正在使用 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.对于所有 arguments,没有候选人至少与所有其他候选人一样好匹配,并且对于某些 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.在所有内置候选中, operator+(double, double)匹配得最好。 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.但这需要对第一个参数进行用户定义的转换,这比第一个参数的所有其他三个用户定义的运算符都差,因此它也无法获胜。

My preferred way to fix this would be to add const to version 1:我首选的解决方法是将const添加到版本 1:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM