Topic: The generic absolute function


Author: "eric_backus@alum.mit.edu" <eric_backus@alum.mit.edu>
Date: Thu, 25 Feb 2010 10:26:14 CST
Raw View
On Feb 7, 4:52 pm, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:
> why there is no generic version for abs too? something like this:
>  template<class _TYPE>
>   _TYPE abs(const _TYPE& t)
>  {
>     return t >= _TYPE(0) ? t : -t;
>  }

I don't really know, but my guess is because the generic version
wouldn't produce the expected answer for complex types.
--
Eric Backus


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Saeed Amrollahi <amrollahi.saeed@gmail.com>
Date: Sun, 7 Feb 2010 18:52:45 CST
Raw View
Hi all

We have several absolute functions in <cmath> (math.h) and <cstdlib>
(stdlib.h).
Some overloaded and some with different names.
 double abs(double); // absolute value; not in C, same as fabs()
 double fabs(double); // absolute value

 int abs(int); // absolute value
 long abs(long); // absolute value (not in C)
 long labs(long); // absolute value

in C++0x. after addition long long integer to fundamental types, we
have
 lon long abs(long long);

I know because for C/C++ compatibility, we have to have all of them.
but, why there is no generic version for abs too? something like this:
 template<class _TYPE>
  _TYPE abs(const _TYPE& t)
 {
    return t >= _TYPE(0) ? t : -t;
 }

I didn't see any attempt to propose this feature.

Thanks in advance for your explanation.

Regards,
 -- Saeed Amrollahi

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]