Inconsistency of <typeinfo>, <initializer_list>, and <compare> in the standard library
Jiang An

Created on 2021-10-23.00:00:00 last changed 1 month ago


Date: 2021-10-24.11:32:41

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 [any.synop], [functional.syn], and [type.index.synopsis].

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

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

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

Date: 2021-10-15.00:00:00

[ 2021-10-24; Daniel comments ]

This issue is related to and depending on LWG 3625.

Date: 2021-10-23.00:00:00

Standard library headers <typeinfo>, <initializer_list>, and <compare> are required for some core language features, as specified in [expr.typeid]/7, [dcl.init.list]/2, and [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.

Date User Action Args
2021-10-24 11:32:41adminsetmessages: + msg12192
2021-10-24 10:58:09adminsetmessages: + msg12191
2021-10-23 00:00:00admincreate