473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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::operator*=(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>::operator*=(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;
}
//-----------------------------------------------------------------------------------------

Jun 23 '06 #1
2 9764
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::operator*=(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>::operator*=(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;
}
//-----------------------------------------------------------------------------------------

Jun 23 '06 #2
* 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>::operator*=(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?
Jun 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peng Yu | last post by:
http://www.digitalmars.com/rtl/complex.html The above the specification of complex.h, but it seem that g++(3.3) hasn't fully support it yet. I wonder where I can find the specifications of...
7
by: Don Tucker | last post by:
Hello, I have the following test program that I can compile with gcc, but when I try to compile with the portland group compiler, pgcc, I get a stream of errors. #include <stdio.h> #include...
17
by: Julian V. Noble | last post by:
Dear C Mavens, I am writing a Computing Prescription for CiSE on complex arithmetic. I would like to be sure that I do not include any gaffes about C99--especially about what is in the header...
3
by: Klaas Vantournhout | last post by:
Hi, I am using CLAPACK to do lots of matrix operations, but this is done on complex matrices. There I also need to work with normal complex operators, I use also the standard complex library. ...
3
by: J.M. | last post by:
I have data in a double array of length 2N, which actually represents complex numbers with real and imaginary parts interlaced. In other words, elements in this array with even indices represents...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.