diff options
author | 2018-08-16 14:40:25 -0700 | |
---|---|---|
committer | 2018-08-16 14:44:18 -0700 | |
commit | 8235e83c442744a1285ce97e5dfc2a6556f9f667 (patch) | |
tree | 53ae08dc9589da67ad35d0b9056721c9e0d8e206 /tensorflow/core/lib | |
parent | 801228d3005b61af2f68223d4769269b1a5a074b (diff) |
Added non-std string support to tensorflow::StringPiece string conversion operator.
PiperOrigin-RevId: 209050419
Diffstat (limited to 'tensorflow/core/lib')
-rw-r--r-- | tensorflow/core/lib/core/stringpiece.h | 16 | ||||
-rw-r--r-- | tensorflow/core/lib/core/stringpiece_test.cc | 4 |
2 files changed, 14 insertions, 6 deletions
diff --git a/tensorflow/core/lib/core/stringpiece.h b/tensorflow/core/lib/core/stringpiece.h index d7ecc44e50..329f115608 100644 --- a/tensorflow/core/lib/core/stringpiece.h +++ b/tensorflow/core/lib/core/stringpiece.h @@ -31,6 +31,7 @@ limitations under the License. #include <string.h> #include <iosfwd> #include <string> +#include <type_traits> #include "tensorflow/core/platform/types.h" namespace tensorflow { @@ -101,11 +102,18 @@ class StringPiece { // > 0 iff "*this" > "b" int compare(StringPiece b) const; - // Converts to `std::basic_string`. - template <typename A> - explicit operator std::basic_string<char, std::char_traits<char>, A>() const { + // Converts to various kinds of strings, including `std::basic_string`. + template <typename S> + explicit operator S() const { + static_assert( + std::is_same<char, typename S::value_type>::value, + "Type mismatch: S must be a string with character type char."); + static_assert( + std::is_same<std::char_traits<char>, typename S::traits_type>::value, + "Type mismatch: S must be a string with traits type " + "std::char_traits<char>."); if (!data()) return {}; - return std::basic_string<char, std::char_traits<char>, A>(data(), size()); + return S(data(), size()); } private: diff --git a/tensorflow/core/lib/core/stringpiece_test.cc b/tensorflow/core/lib/core/stringpiece_test.cc index 952b9eaaaa..e4b489fe17 100644 --- a/tensorflow/core/lib/core/stringpiece_test.cc +++ b/tensorflow/core/lib/core/stringpiece_test.cc @@ -56,8 +56,8 @@ TEST(StringPiece, Ctor) { } TEST(StringPiece, ConversionToString) { - EXPECT_EQ("", std::string(StringPiece(""))); - EXPECT_EQ("foo", std::string(StringPiece("foo"))); + EXPECT_EQ("", string(StringPiece(""))); + EXPECT_EQ("foo", string(StringPiece("foo"))); } } // namespace tensorflow |