Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3876)

Unified Diff: base/strings/string16.h

Issue 1502373009: Allow std::unordered_*. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/strings/string16.h
diff --git a/base/strings/string16.h b/base/strings/string16.h
index e47669c1b565e7cb79bfc275eb5965a9e506cbdf..7e740c9073a9951c1e55e8b94949d26df589a372 100644
--- a/base/strings/string16.h
+++ b/base/strings/string16.h
@@ -29,6 +29,8 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+
+#include <functional>
#include <string>
#include "base/base_export.h"
@@ -182,6 +184,21 @@ BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out);
extern template
class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>;
+// Specialize std::hash for base::string16. Although the style guide forbids
+// this in general, it is necessary for consistency with WCHAR_T_IS_UTF16
+// platforms, where base::string16 is a typedef for std::wstring.
danakj 2016/01/14 21:29:10 s/typedef/type alias/
davidben 2016/01/16 00:22:23 Done.
+namespace std {
+template<>
+struct hash<base::string16> {
+ std::size_t operator()(const base::string16& s) const {
+ std::size_t result = 0;
+ for (base::char16 c : s)
+ result = (result * 131) + c;
+ return result;
+ }
+};
+} // namespace std
+
#endif // WCHAR_T_IS_UTF32
#endif // BASE_STRINGS_STRING16_H_

Powered by Google App Engine
This is Rietveld 408576698