* }
*/
#include <cstddef>
+#include <functional>
#include <new>
#include <stdexcept>
#include <type_traits>
///////////////////////////////////////////////////////////////////////////////
} // namespace folly
+
+// Allow usage of Optional<T> in std::unordered_map and std::unordered_set
+FOLLY_NAMESPACE_STD_BEGIN
+template <class T>
+struct hash<folly::Optional<T>> {
+ size_t operator()(folly::Optional<T> const& obj) const {
+ if (!obj.hasValue()) {
+ return 0;
+ }
+ return hash<typename remove_const<T>::type>()(*obj);
+ }
+};
+FOLLY_NAMESPACE_STD_END
#include <folly/Portability.h>
#include <folly/portability/GTest.h>
-#include <memory>
-#include <vector>
#include <algorithm>
#include <iomanip>
+#include <memory>
#include <string>
#include <type_traits>
+#include <unordered_map>
+#include <vector>
-#include <glog/logging.h>
#include <boost/optional.hpp>
using std::unique_ptr;
EXPECT_TRUE(std::is_trivially_destructible<Optional<int>>::value);
EXPECT_FALSE(std::is_trivially_destructible<Optional<WithDestructor>>::value);
}
+
+TEST(Optional, Hash) {
+ // Test it's usable in std::unordered map (compile time check)
+ std::unordered_map<Optional<int>, Optional<int>> obj;
+ // Also check the std::hash template can be instantiated by the compiler
+ std::hash<Optional<int>>()(none);
+ std::hash<Optional<int>>()(3);
+}
}