1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_CAST_HASH_H_ 16 #define SRC_CAST_HASH_H_ 17 18 #include <cstddef> 19 20 namespace amber { 21 22 /// A hash implementation for types that can trivially be up-cast to a size_t. 23 /// For example, use this as a hasher for an enum whose underlying type is 24 /// an integer no wider than size_t. 25 /// 26 /// The need for this was a defect in the C++11 library, and has been fixed 27 /// in C++14. http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2148 28 template <typename T> 29 struct CastHash { operatorCastHash30 size_t operator()(const T& value) const { return static_cast<size_t>(value); } 31 }; 32 33 } // namespace amber 34 35 #endif // SRC_CAST_HASH_H_ 36