Skip to content

Commit

Permalink
Implement identity_hash (resolves HowardHinnant#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalbersma committed Dec 14, 2016
1 parent bd892bf commit 701d99d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions identity_hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Rein Halbersma 2016
// Distributed under the [Boost Software License, Version 1.0](http://www.boost.org/users/license.html).
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef IDENTITY_HASH_H
#define IDENTITY_HASH_H

#include "endian.h" // endian
#include <cstddef> // size_t

// namespace acme is used to demonstrate example code. It is not proposed.

namespace acme {

class identity_hash
{
std::size_t m_state;
public:
static constexpr xstd::endian endian = xstd::endian::native;
using result_type = std::size_t;

constexpr void operator()(void const* key, std::size_t /* len */) // Throws: Nothing.
{
// Pre-condition: callers must be of the form: identity_hash{}(&x, sizeof(x)).
// In C++1z, we could annotate the len parameter with [[maybe_unused]]
// and check the pre-condition in debug mode with assert(len == sizeof(result_type)).
// In C++14, this requires disabling unused-parameter warnings.
m_state = *static_cast<result_type const*>(key);
}

explicit constexpr operator result_type() const noexcept
{
return m_state;
}
};

} // namespace acme

#endif // IDENTITY_HASH_H

0 comments on commit 701d99d

Please sign in to comment.