microsoft / microsoft/STL

<cmath>: Can we unify overloads and templates?

Open
#1,335 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Related to #189.

<cmath> is one of the STL's most complicated headers (despite the seeming simplicity) because of the "sufficient additional overloads" Standardese in WG21-N4861 [cmath.syn]/2. There's a system of overloads for float and long double (in addition to double provided by the UCRT), and then templates to handle mixed/integral arguments.

There's also some variation - fma and remquo, added in C++11, use a newer technique powered by if constexpr:
https://github.com/microsoft/STL/blob/530bdc5aaa8a21277e1281ad3df8b8d8433b5caa/stl/inc/cmath#L548-L562
(I suspect that float, float arguments are always handled by the separate overload, so there is currently no way to activate it for the template.)

Due to [namespace.std]/6 forbidding forming pointers/references to Standard Library functions (except for "addressable functions"), I believe it may now be unobservable whether the float and long double overloads exist separately. If that's the case, now that if constexpr is available for unconditional use, we should consider eliminating the separate overloads and centralizing everything in remquo-style templates.

Again, due to the complexity, changing anything here is relatively high-risk, but it's not impossible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stl/inc/cmath and compare the existing float and long double overloads with the if constexpr approach used by fma and remquo. Read the related issue #189 and the cited WG21-N4861 [cmath.syn]/2 and [namespace.std]/6 requirements. Done means determining whether the overloads can be centralized without changing observable standard-library behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.