aboutsummaryrefslogtreecommitdiff
path: root/src/cast_hash.h
blob: e02c1487fc2b916761db908e2f625d7b5d0bd43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2018 The Amber Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_CAST_HASH_H_
#define SRC_CAST_HASH_H_

namespace amber {

/// A hash implementation for types that can trivially be up-cast to a size_t.
/// For example, use this as a hasher for an enum whose underlying type is
/// an integer no wider than size_t.
///
/// The need for this was a defect in the C++11 library, and has been fixed
/// in C++14.  http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148
template <typename T>
struct CastHash {
  size_t operator()(const T& value) const { return static_cast<size_t>(value); }
};

}  // namespace amber

#endif  // SRC_CAST_HASH_H_