[vtk-developers] Dashboard problems: ambiguous conversion fabs() call

David Gobbi david.gobbi at gmail.com
Thu Aug 16 11:01:49 EDT 2012


On Thu, Aug 16, 2012 at 8:35 AM, Kyle Lutz <kyle.lutz at kitware.com> wrote:
> On Thu, Aug 16, 2012 at 6:58 AM, David Gobbi <david.gobbi at gmail.com> wrote:
>> On Thu, Aug 16, 2012 at 7:39 AM, Kyle Lutz <kyle.lutz at kitware.com> wrote:
>>>
>>> Also, did you mean std::abs()? std::fabs() is not overloaded for the
>>> integral types and std::abs() should be preferred anyway.
>>
>> In code that is only meant to be used for floating-point numbers, I
>> prefer std::fabs() because I _like_ the fact that it produces an error
>> if an integer slips in. But for vtkTuple, where both ints and floats
>> are possible, std::abs() is the clear winner.
>
> I see your point.
>
> However, you will not receive an error when you pass an integer to
> std::fabs(). The integer will just be converted to a floating-point
> type before being passed to the function.
>
> For example try the following code:
>
> int i = -42;
> std::cout << std::fabs(i) << std::endl;

If std::fabs() only has overloads for float, double, long double
(as is the case on some compilers) then your example code
will fail to compile.  In general I don't think that std::fabs()
is templated, it's just overloaded for different arg types, in
which case it will give the same error as the following code:

float myfabs(float x) { return std::fabs(x); }
double myfabs(double x) { return std::fabs(x); }

int main(int argc, char *argv[])
{
  int i = -42;
  std::cout << myfabs(i) << std::endl;

  return 0;
}

 - David



More information about the vtk-developers mailing list