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

2791. string_view objects and strings should yield the same hash values

Section: 23.3.6 [string.view.hash] Status: Resolved Submitter: Nicolai Josuttis Opened: 2016-11-09 Last modified: 2016-11-21

Priority: Not Prioritized

View all issues with Resolved status.


Under certain conditions we want to be able to use string_view instead of string. As a consequence both types should behave the same as long we talk about value specific behavior (if possible). For this reason, we should require that both strings and string_view yield the same hash values.

Should be solved in C++17 to ensure that following this use does not require to change hash values (which is allowed but possibly unfortunate).

[2016-11-12, Issaquah]

Resolved by P0513R0

Proposed resolution:

This wording is relative to N4606.

A more formal formulation would be:

For any sv of type SV (being string_view, u16string_view, u32string_view, or wstring_view) and s of the corresponding type S (being string, u16string, u32string, or wstring), hash<SV>()(sv) == hash<S>()(s).

  1. Edit 23.3.6 [string.view.hash] as indicated:

    template<> struct hash<string_view>;
    template<> struct hash<u16string_view>;
    template<> struct hash<u32string_view>;
    template<> struct hash<wstring_view>>;

    -1- The template specializations shall meet the requirements of class template hash (22.10.19 [unord.hash]). The hash values shall be the same as the hash values for the corresponding string class (23.4.6 [basic.string.hash]).