This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Strange conversion behavior in g++
- From: Martin Reinecke <martin at MPA-Garching dot MPG dot DE>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 04 Jul 2002 14:10:52 +0200
- Subject: Strange conversion behavior in g++
- Organization: Max-Planck-Institut fuer Astrophysik
Hi,
I've run across the following oddity and I'm not sure if it is a problem
with g++ or a strangeness in the C++ standard.
Consider the following program:
#include <complex>
using namespace std;
class foo {
public:
operator double() const { return 0.; }
operator complex<double>() const { return(complex<double>(0.,0.)); }
};
int main()
{
foo bar;
static_cast<complex<double> >(bar);
}
Compilation with a current g++ produces the following output:
bugdemo.cc: In function `int main()':
bugdemo.cc:14: error: call of overloaded `complex(foo&)' is ambiguous
/afs/mpa/data/martin/ugcc/include/c++/3.2/complex:738: error: candidates are:
std::complex<double>::complex(const std::complex<double>&)
/afs/mpa/data/martin/ugcc/include/c++/3.2/complex:791: error:
std::complex<double>::complex(double = 0x000000000000000000000000, double =
0x000000000000000000000000)
I know that the conversion from a "foo" to a "complex<double>" can be done
via a "double", but the statement
static_cast<complex<double> >(bar);
looks pretty explicit and unambiguous to me.
Could you please explain me why this piece of code is ambiguous, and point
out a correct solution?
Thanks in advance,
Martin Reinecke