This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Over-zealous overloading errors (g++)
- From: Russell Edwards <redwards at astro dot uva dot nl>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 19 Mar 2003 10:47:08 +0100
- Subject: Over-zealous overloading errors (g++)
Hello,
since the bug report people don't seem to think this is a bug,
I'm hoping someone can enlighten me as to what the best way to
avoid the following related errors are.
Consider this innocuous looking code fragment:
#include <cmath>
void f()
{
float a, b=1.0;
a = std::pow(b, 1.5);
}
I would (perhaps naively) think the compiler should silently and happily
promote the first argument to pow to a double and go upon its way.
Maybe an annoying compiler could put out a warning message. Well gcc 3.2
gives an _error_ (or two of them actually).
ptest.cc: In function `void f()':
ptest.cc:6: choosing `double pow(double, double)' over `float std::pow(float,
int)'
ptest.cc:6: because worst conversion for the former is better than worst
conversion for the latter
ptest.cc:6: choosing `double pow(double, double)' over `float std::pow(float,
float)'
ptest.cc:6: because worst conversion for the former is better than worst
conversion for the latter
Now I could go and put (float)s or (doubles) all over the place but I
really would rather not as it seems an unnecessarily uglification.
What is the proper way to deal with this?
On a related note, I get the following error in a million different
places when I try to compile my code with gcc 3.2
pbird.cc:102: choosing `PhysData::PhysDataPoint<T> PhysData::operator*(const
PhysData::PhysDataPoint<T>&, double) [with T = double]' over `operator*'
pbird.cc:102: because worst conversion for the former is better than worst
conversion for the latter
The code is much too big to include here but I'm hoping someone can
hazard a guess as to what the problem is here, what exactly is the
compiler talking about with its second "operator*"? Why doesn't it
give the full type-specified name for the second operator?
The only * operators I define are
template <typename T> PhysDataPoint<T> operator*(const PhysDataPoint<T>&,
const PhysDataPoint<T>&);
template <typename T> PhysDataPoint<T> operator*(const PhysDataPoint<T>&,
double);
The above error happens when I multiply a PhysDataPoint by an int
or a float. The second op is a convenience operator, PhysDataPoint stores
a value along with info about its units, and frequently one wants to
multiply or divide by a dimensionless number one has as an int or a float,
without all the ugliness of constructing a whole PhysDataPoint for it.
Could the problem be that I have a constructor PhysDataPoint(T _val=T())
and that it therefore doesn't know whether to construct one for me or
to promote to a double and use the second operator?
Any help or pointers to general principles for avoiding these kinds of
headaches would be greatly appreciated!
thanks
Russell