5

Many of the new functions brought in C++11 by TR1 have ugly C-like signatures. For instance, quoting Boost's TR1's documentation (http://www.boost.org/doc/libs/1_52_0/doc/html/boost_tr1/subject_list.html#boost_tr1.subject_list.special):

// [5.2.1.1] associated Laguerre polynomials:
double assoc_laguerre(unsigned n, unsigned m, double x);
float assoc_laguerref(unsigned n, unsigned m, float x);
long double assoc_laguerrel(unsigned n, unsigned m, long double x);

Obviously one would have preferred some templated implementation (which is actually the "native" signature of these functions in Boost), or at least some overloading instead of several identifiers.

I can understand that aiming at C compatibility means supporting these identifiers, but then this a nuisance for pure C++ speakers. In addition to <cmath>, there could be some <math>, with more natural interfaces.

What is it that I am missing (besides, possibly, some previously asked questions)?

akim
  • 8,255
  • 3
  • 44
  • 60
  • [This may be relevant.](http://stackoverflow.com/a/12063878/500104) – Xeo Feb 01 '13 at 10:01
  • The user base for those functions is already rather small, I guess no one thought it would be worth to require template implementations (also non-templates are much easier to maintain in form of fast inline assembly stuff) – PlasmaHH Feb 01 '13 at 10:02
  • @Xeo Thanks, I completely missed the paragraphs 3, 4, and 5 from [5.2.1]: they do require that overloads be added for `float` and `long double` based on the `double` signatures (the one with the appropriate name). – akim Feb 01 '13 at 10:08
  • 1
    Also see Jonathan Wakely's [Why `` is more complicated than you might think](https://developers.redhat.com/blog/2016/02/29/why-cstdlib-is-more-complicated-than-you-might-think/) from the Red Hat blogs. Wakely is one of GCC's C++ standard library maintainers. I think `` vs `` is a much more interesting case study because C++ requires the three overloads for many functions. – jww Aug 01 '17 at 19:47

1 Answers1

4

I don't know about Boost, but all of the standard functions in <cmath> have overloads for the three standard types, so you have e.g.:

double cos(double);
float cos(float);
long double cos(long double);

in place of C's:

double cos(double);
float cosf(float);
long double cosl(long double);

I'm not quite sure why you want a function template instead of overloads. For most mathematic functions, no generic implementation is possible; a correct implementation will depend on precision, rounding rules, etc. which are different for each type. So the alternative would be a template function without a generic implementation, and three specializations. And what does that buy you over the "simpler" overloaded functions?

James Kanze
  • 150,581
  • 18
  • 184
  • 329