Skip to content

hash_combine, hash_range, and hash_value can be constexpr #16

@saki7

Description

@saki7

https://github.com/boostorg/container_hash/blob/develop/include/boost/container_hash/hash.hpp

Since most part of internal calculations are based on integer arithmetic and bitwise operations, I believe those three functions and all their variants can be marked constexpr today. Can someone please enlighten me, why the standard std::hash<T>::operator() and proposed std::hash_combine() in P0814R2 is not constexpr? (backward-compatibility, or other reasons?)

If we are to provide constexpr-ness, there are two cases of implementations:

  • When user is using standard later or equal to C++20
    • In this case, non-constexpr intrinsic like _rotl (defined for BOOST_FUNCTIONAL_HASH_ROTL32) can be replaced with std::rotl
  • When user is using C++14 to C++17 and one prefers constexpr
    • The library can fallback to bitwise implementation (which is always constexpr)
    • Currently Boost.ContainerHash provides no option for this case. I think new macro like BOOST_FUNCTIONAL_HASH_USE_CONSTEXPR is needed.

refs: cplusplus/papers#253

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions