Switch includes of PThread to the portability header
[folly.git] / folly / Optional.h
index acd613ab6bf57b9fdef081e30ee4f141afe5c015..5350b2ee5e44d69c55868c0a96e2e24d286a16fb 100644 (file)
@@ -54,6 +54,7 @@
  *  }
  */
 #include <cstddef>
+#include <functional>
 #include <new>
 #include <stdexcept>
 #include <type_traits>
@@ -265,7 +266,7 @@ class Optional {
 
   struct StorageTriviallyDestructible {
     // The union trick allows to initialize the Optional's memory,
-    // so that compiler/tools don't complain about unitialized memory,
+    // so that compiler/tools don't complain about uninitialized memory,
     // without actually calling Value's default constructor.
     // The rest of the implementation enforces that hasValue/value are
     // synchronized.
@@ -418,3 +419,16 @@ template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
 ///////////////////////////////////////////////////////////////////////////////
 
 } // 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