Document Number

P1328R1

Date

2021-05-01

Project

Programming Language C++

Audience

Library Working Group

Summary

This paper proposes making std::type_info::operator== constexpr.

Revision History

Revision R1

  • Rebased against N4885

  • Added a feature macro

Summary

typeid is currently allowed in constant expressions, but the resulting std::type_info object is unusable as it has no constexpr member functions.

This paper proposes std::type_info::operator== be made constexpr, enabling practical, rather than theoretical, use of typeid in constant expressions.

Proposed Changes

(All edits are relative to N4885.)

Change 17.7.3 [type.info] as follows:

  • namespace std {
      class type_info {
      public:
        virtual ~type_info();
        constexpr bool operator==(const type_info& rhs) const noexcept;
        bool before(const type_info& rhs) const noexcept;
        size_t hash_code() const noexcept;
        const char* name() const noexcept;
        type_info(const type_info& rhs) = delete; // cannot be copied
        type_info& operator=(const type_info& rhs) = delete; // cannot be copied
      };
    }
    constexpr bool operator==(const type_info& rhs) const noexcept;

Add to 17.3.2 [version.syn]:

  • #define __cpp_lib_constexpr_typeinfo   202105L  // also in <typeinfo>

with the value appropriately replaced with the date of adoption.

-- end