This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Strange conversion behavior in g++


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]