This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

### 3624. Inconsistency of `<typeinfo>`, `<initializer_list>`, and `<compare>` in the standard library

Section: 17.7 [support.rtti], 17.10 [support.initlist], 17.11 [cmp] Status: New Submitter: Jiang An Opened: 2021-10-23 Last modified: 2021-10-24

Priority: 3

View all issues with New status.

Discussion:

Standard library headers `<typeinfo>`, `<initializer_list>`, and `<compare>` are required for some core language features, as specified in 7.6.1.8 [expr.typeid]/7, 9.4.5 [dcl.init.list]/2, and 7.6.8 [expr.spaceship]/8. In C++11 (via N2930), every header that has dependency on `std::initializer_list` is required to include `<initializer_list>`. The similar requirements are added for `operator<=>` and `<compare>` in C++20 (via LWG 3330).

As N2930 and LWG3330 have been adpoted, IMO there are some inconsistencies in the standard library now:

- No operation is done for `<typeinfo>`, although `<typeindex>` (`std::type_index`), `<functional>` (`std::function`, since C++11), and `<any>` (`std::any`) have dependency on `std::type_info`;

- `<iterator>` has dependency on `std::initializer_list` since C++14/LWG 2128 (the `std::rbegin` overload and its friends), but it is not required to include `<initializer_list>`;

- `<stacktrace>` is not required to include `<compare>` while it provides operator `<=>` overloads.

The situation may be quite serious for `std::type_index`. Perhaps no expected operation on `std::type_index` is guaranteed to work when only `<typeindex>` but not `<typeinfo>` is included.

libc++, libstdc++, and MSVC STL include `<typeinfo>` and `<initializer_list>` when the required standard interface depends on them. I think we should standardize the existing practice (except that `<stackstrace>` has not been implemented now) to reduce uncertainty for users.

[2021-10-24; Daniel comments]

This issue is related to and depending on LWG 3625.

[2022-01-29; Reflector poll]

Set priority to 3 after reflector poll.

Proposed resolution:

This wording is relative to N4901.

> [Drafting note: The proposed wording below contains one conditional change, it is therefore depending upon LWG 3625.]

1. Add `#include <typeinfo>` to 20.8.2 [any.synop], 20.14.2 [functional.syn], and 20.17.1 [type.index.synopsis].

2. Add `#include <initializer_list>` to 23.2 [iterator.synopsis].

3. Add `#include <compare>` to 20.21.2 [stacktrace.syn].

4. If we decide to add range access function templates (23.7 [iterator.range]) to `<stacktrace>`, we should also add `#include <initializer_list>` to 20.21.2 [stacktrace.syn].