Skip to content

Using std::hash through <typeindex> does not compile for builtin arithmetic types #129723

@johnfranklinrickard

Description

@johnfranklinrickard

As far as I am aware (hope I am not mistaken), this should be valid C++ code.

#include <typeindex>
std::size_t test(int i) { return std::hash<int>()(i); }

From the C++ draft http://wg21.link/N4950:
In [unord.hash] 2:

Each header that declares the template hash provides enabled specializations of hash for nullptr_t and all
cv-unqualified arithmetic, enumeration, and pointer types

And in [type.index.synopsis] it is mentioned that the header <typeindex> also declares the template std::hash.

In my opinion the msvc STL has it correctly in this case and the code compiles correctly.
Both libc++ and libstdc++ currently do not work for this code snippet and reject it with implicit instantiation of undefined template 'std::hash<int>'.

Godbolt link

I only checked it for <typeindex>, but this could affect more headers where std::hash is supposed to be defined as mentioned in cppreference.

Under the assumption that this is correct C++, would it be possible to fix these includes for the LLVM standard library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions