Skip to content

Instantly share code, notes, and snippets.

@Masstronaut
Created September 29, 2017 00:44
Show Gist options
  • Save Masstronaut/7e5ad7c42347c8793918c6026ec2e4b7 to your computer and use it in GitHub Desktop.
Save Masstronaut/7e5ad7c42347c8793918c6026ec2e4b7 to your computer and use it in GitHub Desktop.
trivial implementation of a basic hash pointer
template<typename T, typename Hasher = ::std::hash<T>>
class hash_ptr {
public:
using value_type = T;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = const value_type &;
using pointer = T*;
using hash_type = typename Hasher::result_type;
//using hash_type = typename decltype( std::declval<Hasher>( )( std::declval<value_type>( ) ) );
hash_ptr( pointer ptr, Hasher hasher = Hasher{} )
: m_p( std::forward<pointer>( ptr ) )
, m_hash( hasher( *ptr ) ) { }
reference operator*( ) { return *m_p; }
const_reference operator*( ) const { return *m_p; }
pointer operator->( ) {
return m_p;
}
bool good( ) const {
return m_p != nullptr && m_hash == Hasher{}( *m_p );
}
operator bool( ) const {
return good( );
}
operator pointer( ) const {
return m_p;
}
hash_type hash( ) const { return m_hash; }
private:
pointer m_p;
hash_type m_hash;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment