Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > <complex> : no match for 'operator*' // conversion operator double()

Reply
Thread Tools

<complex> : no match for 'operator*' // conversion operator double()

 
 
Arvid Requate
Guest
Posts: n/a
 
      06-23-2006
Hello,

I'd like to understand why the following code does not compile. It
looks like a strangeness in connection with overload resolution for the
<complex> header:
The conversion operator double() of class B is called for the member
complex:perator*=(double) as expected, but not for operator*(complex,
double).

The effect is, that the template matching (or overload resolution)
fails. Error message:
complex_double_conversion.cpp: In function 'int main()':
complex_double_conversion.cpp:19: error: no match for 'operator*' in 'z
* x'

Tested with g++ 4.0.2 20050901 (prerelease) on (SUSE Linux) and others.
Do <you> know a reason/remedy for this behaviour? Thanks in advance.

//------------------------complex_double_conversion.cpp-------------------------
#include<complex>

class B {
double v;
public:
operator double() const { return v; } // conversion operator
B(double _v) : v(_v) {}
};

int main() {
std::complex<double> z(0,1);
B x(0.5);

// next line works due to complex<_Tp>&
complex<_Tp>:perator*=(const _Tp&)
// the conversion operator of class B is used
z*=x;

// the next line does not compile
std::complex<double> y( z*x );

// only with cast: z*((double) x)
//
// although in <complex> there is
// template<typename _Tp>
// inline complex<_Tp> operator*(const complex<_Tp>& __x, const
_Tp& __y)
// { return complex<_Tp> (__x) *= __y; }
//
return 0;
}
//-----------------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Tom Widmer
Guest
Posts: n/a
 
      06-23-2006
Arvid Requate wrote:
> Hello,
>
> I'd like to understand why the following code does not compile. It
> looks like a strangeness in connection with overload resolution for the
> <complex> header:
> The conversion operator double() of class B is called for the member
> complex:perator*=(double) as expected, but not for operator*(complex,
> double).
>
> The effect is, that the template matching (or overload resolution)
> fails. Error message:
> complex_double_conversion.cpp: In function 'int main()':
> complex_double_conversion.cpp:19: error: no match for 'operator*' in 'z
> * x'
>
> Tested with g++ 4.0.2 20050901 (prerelease) on (SUSE Linux) and others.
> Do <you> know a reason/remedy for this behaviour? Thanks in advance.


The operator* function you want to call is a non-member as follows:
template<class T>
complex<T> operator*(const complex<T>& lhs, const T& rhs);

Template argument deduction fails because it requires that the arguments
are exact matches for the deduced parameter types (with a few
exceptions), but B is not similar enough to double const& (user defined
conversions aren't considered).

The easiest fix is to write a suitable function (in the same namespace
as B so that it is found by ADL), something like:

complex<double> operator*(const complex<double>& lhs, const B& rhs)
{
return lhs * static_cast<double>(rhs);
}

Tom

>
> //------------------------complex_double_conversion.cpp-------------------------
> #include<complex>
>
> class B {
> double v;
> public:
> operator double() const { return v; } // conversion operator
> B(double _v) : v(_v) {}
> };
>
> int main() {
> std::complex<double> z(0,1);
> B x(0.5);
>
> // next line works due to complex<_Tp>&
> complex<_Tp>:perator*=(const _Tp&)
> // the conversion operator of class B is used
> z*=x;
>
> // the next line does not compile
> std::complex<double> y( z*x );
>
> // only with cast: z*((double) x)
> //
> // although in <complex> there is
> // template<typename _Tp>
> // inline complex<_Tp> operator*(const complex<_Tp>& __x, const
> _Tp& __y)
> // { return complex<_Tp> (__x) *= __y; }
> //
> return 0;
> }
> //-----------------------------------------------------------------------------------------
>

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-23-2006
* Arvid Requate:
>
> I'd like to understand why the following code does not compile.

[snip]

> //------------------------complex_double_conversion.cpp-------------------------
> #include<complex>
>
> class B {
> double v;
> public:
> operator double() const { return v; } // conversion operator
> B(double _v) : v(_v) {}
> };
>
> int main() {
> std::complex<double> z(0,1);
> B x(0.5);
>
> // next line works due to complex<_Tp>&
> complex<_Tp>:perator*=(const _Tp&)
> // the conversion operator of class B is used
> z*=x;
>
> // the next line does not compile
> std::complex<double> y( z*x );
>
> // only with cast: z*((double) x)
> //
> // although in <complex> there is
> // template<typename _Tp>
> // inline complex<_Tp> operator*(const complex<_Tp>& __x, const
> _Tp& __y)
> // { return complex<_Tp> (__x) *= __y; }
> //
> return 0;
> }
> //-----------------------------------------------------------------------------------------


Template parameter matching does not consider user-defined conversions:
in general types must match exactly (sort of, there's a bit of looseness
in cv-qualification and reference types and so on).

Consider:

template< typename T >
struct Complex { Complex( T = 0, T = 0 ) {} };

template< typename T >
Complex<T> operator*( Complex<T> const&, Complex<T> const& )
{ return Complex<T>(); }

class B
{
double v;
public:
B( double _v ) : v( _v ) {}
operator Complex<double> () const { return v; }
};

int main()
{
Complex<double> z(0,1);
B x(0.5);

// The next line does not compile, not exact match:
z*x;
}

However, adding

template< typename T >
Complex<T> operator*( Complex<T> const& a, B const& b )
{ return a*Complex<T>(b); }

provides an exact match and the code compiles.

A bit more general, adding instead

template< template<class> class C, typename T >
C<T> operator*( C<T> const& a, B const& b )
{ return a*C<T>(b); }

also provides an exact match and the code compiles.

I tried this fix with your original code and it compiles with MSVC 7.1.
However, you'll probably also have to support the opposite argument
order, and other operators such as '+'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
operator << and conversion operator cronusf C++ 12 10-28-2010 09:21 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
error: no match for ‘operator<<’ in ‘std::cout.std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](k) << dispMyarr(k)’ curiousEngine C++ 1 05-09-2008 09:34 AM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments