From: Phil Willoughby Date: Wed, 17 May 2017 11:05:52 +0000 (-0700) Subject: Tweak basic_fbstring construction from std types X-Git-Tag: v2017.05.22.00~11 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=6c244d162cd8f6e598202146c03b9bf13499d113 Tweak basic_fbstring construction from std types Summary: Previously we defined an assignment from std::string on all basic_fbstring specialisations, but we don't actually want to do that because it's nonsense to construct a basic_fbstring from an std::string. Equally it's not nonsense to construct one of those from an std::u16string but the previous code didn't allow it. We now permit basic_fbstring to be constructed or assigned-to from std::basic_string. The character type and traits must match but the allocator is permitted to vary. Background on my determination that the allocator type was unimportant and could be disregarded: In part this is because C++17 made the same choice for basic_string_view. Another factor was C++17's std::pmr::string (it's a std::string with a different allocator) which I thought should be convertible to fbstring in the same way as std::string. Reviewed By: Gownta Differential Revision: D5060569 fbshipit-source-id: f8984c528b76356240970c67916c58995d3f228d --- diff --git a/folly/FBString.h b/folly/FBString.h index 45ba6cb5..5db1e61e 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -1141,9 +1141,9 @@ public: #ifndef _LIBSTDCXX_FBSTRING // This is defined for compatibility with std::string - /* implicit */ basic_fbstring(const std::string& str) - : store_(str.data(), str.size()) { - } + template + /* implicit */ basic_fbstring(const std::basic_string& str) + : store_(str.data(), str.size()) {} #endif basic_fbstring(const basic_fbstring& str, @@ -1205,13 +1205,14 @@ public: #ifndef _LIBSTDCXX_FBSTRING // Compatibility with std::string - basic_fbstring & operator=(const std::string & rhs) { + template + basic_fbstring& operator=(const std::basic_string& rhs) { return assign(rhs.data(), rhs.size()); } // Compatibility with std::string - std::string toStdString() const { - return std::string(data(), size()); + std::basic_string toStdString() const { + return std::basic_string(data(), size()); } #else // A lot of code in fbcode still uses this method, so keep it here for now.