简体   繁体   English

C ++运算符重载分辨率含糊不清

[英]C++ operator overloading resolution ambiguity

Am trying to move an antique C++ code base from gcc 3.2 to gcc 4.1, as I have run into a few issues. 我试图将老式的C ++代码库从gcc 3.2迁移到gcc 4.1,因为我遇到了一些问题。 Of all the issues, the following is left me clueless (I guess I spent too much time with Java or I might have forgotten the basics of C++ :-P ). 在所有问题中,以下内容使我毫无头绪(我想我花了太多时间在Java上,或者我可能忘记了C ++的基本知识:-P)。

I have a template class 我有一个模板课

template < class T > class XVector < T >
{
   ...
   template < class T > T
   XVector < T >::getIncrement ()
   {
       ...
   }  
   template < class T > int
   XVector < T >::getValue (size_t index, T& value)
   {
      ...
      //The problematic line
      value = (T) (first_value + getIncrement())
                  * (long) (index - first_index);
      ....
   }
}

This class is based on the STL std::vector. 此类基于STL std :: vector。 I have a second class TypeValue and its defined as below which can hold one of int, long and their unsigned variants, float, double, std::string. 我有第二个类TypeValue及其定义如下,它可以容纳int,long及其无符号变体之一,float,double,std :: string中的一个。 Also overloads almost all possible operators. 也使几乎所有可能的运算符过载。

class TypeValue
{
    union
    {
        long* _int_value;
        double* _real_value;
        string* _text_value;
    } _value;

    TypeValue();
    explicit TypeValue(long _long);
    explicit TypeValue(int _int);
    explicit TypeValue(unsigned long _ulong);
    ...
    //similarly for all the remaining supported types.
    TypeValue(const TypeValue& ) //Copy constructor

    virtual TypeValue& operator=(const TypeValue &rhs);
    TypeValue& operator+ (TypeValue& )const;
    TypeValue& operator* (TypeValue& )const;
    ...
    //For all the basic operators

    operator long() const;
    operator int() const;
    operator unsigned long() const;
    operator unsigned int() const;
    ...

}

And finally I have another class, lets call it the build_breaker, which creates an object as XVector < TypeValue > a_variable; 最后,我还有另一个类,称之为build_breaker,它创建一个对象为XVector < TypeValue > a_variable; . Now when I compile this on gcc 3.2 this compiles without any problems. 现在,当我在gcc 3.2上进行编译时,此编译将没有任何问题。 But when I try compiling this on gcc 4.1 I get errors saying ambigous overload for operator* in the class XVector and the candidates being 但是,当我尝试在gcc 4.1上进行编译时,我得到了错误,提示类XVector中的operator*大量重载,而候选对象是

operator*(long int, long int) 
operator*(int, long int) 
operator*(long unsigned int, long int) 
operator*(unsigned int, long int) 
operator*(double, long int) 
operator*(float, long int)

If the compiler said it had problems finding a match for T * long, that would have made sense, but, why is it trying to typecast it to native type and then perform the arithmetic operation? 如果编译器说在找到T * long的匹配项时遇到问题,那是有道理的,但是,为什么它试图将其类型转换为本机类型,然后执行算术运算呢? Please help me on this. 请帮我。

Thanks in advance. 提前致谢。

The second operand type is long [int] . 第二种操作数类型是long [int] The first is TypeValue , I expect, but there is no operator * that takes those two exact types. 我希望第一个是TypeValue ,但是没有运算符*可以接受这两个确切类型。 There are lots of other type combinations for that operator, though, which the compiler can select by doing an implicit conversion on the first operand. 但是,该运算符还有许多其他类型组合,编译器可以通过对第一个操作数进行隐式转换来选择这些组合。 The language allows the compiler to do that to try to find a match. 该语言允许编译器执行此操作以尝试找到匹配项。

But which of the many conversions should it choose? 但是,它应该选择众多转换中的哪一个? The compiler has no way to choose whether int is better than long int . 编译器无法选择int是否比long int (You might argue that since the second operand is long int , that should be the preferred conversion target, but that's not the way things work.) (您可能会争辩说,由于第二个操作数是long int ,所以它应该是首选的转换目标,但这不是事情的工作方式。)

So, some advice: First, don't supply so many implicit conversions. 因此,有一些建议:首先,不要提供太多的隐式转换。 Since the class can only hold long , double , and string , those are the only three conversions I'd supply. 由于该类只能容纳longdoublestring ,因此这些是我提供的仅有的三个转换。 That alone probably won't solve your problem, but it may reduce the size of the error output and make other things more manageable. 仅此一项可能无法解决您的问题,但可能会减少错误输出的大小并使其他内容更易于管理。

Instead of converting (index - first_index) to type long , consider converting it to type T (ie, TypeValue ) instead, since that seems to be the operation you really wanted to perform in the first place. 不要将(index - first_index)转换为long类型,而应考虑将其转换为T (即TypeValue )类型,因为这似乎是您真正想执行的操作。

I would change all the conversion operators to named functions so that: 我将所有转换运算符更改为命名函数,以便:

operator long() const;

becomes 变成

long ToLong() const;

Implicit conversiopns via cast operators cause all sorts of problems, and my own programming standards bans their use. 通过强制转换运算符进行的隐式对话会引起各种问题,而我自己的编程标准禁止使用它们。

我们需要知道什么是T。看起来您正在使用某种类型(例如,无符号字符)实例化一个XVector,该类型可以转换为您看到的所有这些类型,并且编译器不知道该选择哪种类型。 。

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

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