Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ operator overloading resolution ambiguity
    text
    copied!<p>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. 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 ).</p> <p>I have a template class</p> <pre><code>template &lt; class T &gt; class XVector &lt; T &gt; { ... template &lt; class T &gt; T XVector &lt; T &gt;::getIncrement () { ... } template &lt; class T &gt; int XVector &lt; T &gt;::getValue (size_t index, T&amp; value) { ... //The problematic line value = (T) (first_value + getIncrement()) * (long) (index - first_index); .... } } </code></pre> <p>This class is based on the 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. Also overloads almost all possible operators.</p> <pre><code>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&amp; ) //Copy constructor virtual TypeValue&amp; operator=(const TypeValue &amp;rhs); TypeValue&amp; operator+ (TypeValue&amp; )const; TypeValue&amp; operator* (TypeValue&amp; )const; ... //For all the basic operators operator long() const; operator int() const; operator unsigned long() const; operator unsigned int() const; ... } </code></pre> <p>And finally I have another class, lets call it the build_breaker, which creates an object as <code>XVector &lt; TypeValue &gt; a_variable;</code>. Now when I compile this on gcc 3.2 this compiles without any problems. But when I try compiling this on gcc 4.1 I get errors saying ambigous overload for <code>operator*</code> in the class <code>XVector</code> and the candidates being</p> <pre> 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) </pre> <p>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? Please help me on this.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload