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

### 2144. Missing `noexcept` specification in `type_index`

Section: 20.17 [type.index] Status: C++14 Submitter: Daniel Krügler Opened: 2012-03-18 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [type.index].

View all issues with C++14 status.

Discussion:

The class type `type_index` is a thin wrapper of `type_info` to adapt it as a valid associative container element. Similar to `type_info`, all member functions have an effective `noexcept(true)` specification, with the exception of `hash_code()` and `name()`. The actual effects of these functions is a direct call to `type_info`'s `hash_code()` and `name` function, but according to 17.7 [support.rtti] these are both `noexcept` functions, so there is no reason for not declaring them as `noexcept`, too. In fact, one of the suggested changes of the original proposing paper N2530 specifically was to ensure that `type_info` would get a `hash_code()` function that guarantees not to throw exceptions (during that time the `hash` requirements did not allow to exit with an exception). From this we can conclude that `type_index::hash_code()` was intended to be nothrow.

It seems both consistent and technically simply to require these functions to be `noexcept`.

[2013-03-15 Issues Teleconference]

Moved to Tentatively Ready.

[2013-04-20 Bristol]

Proposed resolution:

This wording is relative to N3376.

1. Modify the class `type_index` synopsis, 20.17.2 [type.index.overview] as indicated:

   >     namespace std {
   >       class type_index {
   >       public:
   >         type_index(const type_info& rhs) noexcept;
   >         bool operator==(const type_index& rhs) const noexcept;
   >         bool operator!=(const type_index& rhs) const noexcept;
   >         bool operator< (const type_index& rhs) const noexcept;
   >         bool operator<= (const type_index& rhs) const noexcept;
   >         bool operator> (const type_index& rhs) const noexcept;
   >         bool operator>= (const type_index& rhs) const noexcept;
   >         size_t hash_code() const noexcept;
   >         const char* name() const noexcept;
   >       private:
   >         const type_info* target; // exposition only
   >         // Note that the use of a pointer here, rather than a reference,
   >         // means that the default copy/move constructor and assignment
   >         // operators will be provided and work as expected.
   >       };
   >     }

1. Modify the prototype definitions in 20.17.3 [type.index.members] as indicated:

   >     size_t hash_code() const noexcept;

   > > -8- Returns: `target->hash_code()`

   >     const char* name() const noexcept;

   > > -9- Returns: `target->name()`