Re: Implicit conversion for operator
On Jul 25, 7:27 am, pikalaw <pika...@gmail.com> wrote:
On Jul 24, 11:19 pm, Stephen Horne <sh006d3...@blueyonder.co.uk>
wrote:
On Fri, 24 Jul 2009 19:24:06 -0700 (PDT), pikalaw <pika...@gmail.com>
wrote:
An update:
Even this simplified code does not compile.
complex<long > y;
const complex<long > k(0,1);
y = 2 * k;
But if I replace the constant 2 with 2L, the long version,
then it compiles.
When you use the literal 2, I suspect two conversions are
needed...
2 -> 2L (from integer to long)
2L -> <complex 2> (from long to complex)
IIRC, conversions are only done implicitly if they can be
achieved in a single step.
No. The restriction only applies to user defined conversions.
If complex wasn't a template (or if the called function wasn't a
template), then there would be no problem.
I checked the stl source, the template defines the operator template
complex<T> operator*(const T &, const complex<T>);
So, I had expected this template to instantiate into
complex<long> operator*(const long &, const complex<long> &)
which with one implicit conversion from int to long would
allow '2 * k' be called.
The problem is precisely that type deduction doesn't succeed for
the template. Type deduction using the first argument results
in T == int, and type deduction using the second argument
results in T == long. The compiler cannot choose between them.
(In type deduction, very few conversions are considered;
considering all conversions would far too often result in
ambiguity.) And since type deduction fails, there is no
instantiation (i.e. no function) for the compiler to do overload
resolution on.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34