Commit 35271eac authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

implement implicit conversion from FixedString to std::string_view

Summary:
This makes it possible to pass `FixedString` objects to APIs that accept
`std::string_view`.  `FixedString` already had implicit conversion to
`std::string` and `folly::StringPiece`, this simply implements parity for
`string_view`.

Reviewed By: yfeldblum

Differential Revision: D26940391

fbshipit-source-id: 5f411ef9a901e0855593b090f1cbe3345971a140
parent 47e384c5
......@@ -36,6 +36,10 @@
#include <folly/lang/Ordering.h>
#include <folly/portability/Constexpr.h>
#if FOLLY_HAS_STRING_VIEW
#include <string_view>
#endif
namespace folly {
template <class Char, std::size_t N>
......@@ -774,6 +778,16 @@ class BasicFixedString : private detail::fixedstring::FixedStringBase {
return std::basic_string<Char>{begin(), end()};
}
#if FOLLY_HAS_STRING_VIEW
/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
* Conversion to std::basic_string_view<Char>
* \return `std::basic_string_view<Char>{begin(), end()}`
*/
/* implicit */ constexpr operator std::basic_string_view<Char>() const {
return std::basic_string_view<Char>{begin(), size()};
}
#endif
// Think hard about whether this is a good idea. It's certainly better than
// an implicit conversion to `const Char*` since `delete "hi"_fs` will fail
// to compile. But it creates ambiguities when passing a FixedString to an
......
......@@ -653,7 +653,7 @@ TEST(FixedStringReverseIteratorTest, ConstexprReverseIteration) {
#include <folly/Range.h>
TEST(FixedStringConversionTest, ConversionToFollyRange) {
// The following declaraction is static for compilers that haven't implemented
// The following declaration is static for compilers that haven't implemented
// the resolution of:
// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1454
static constexpr folly::FixedString<16> tmp{"This is a string"};
......@@ -661,3 +661,12 @@ TEST(FixedStringConversionTest, ConversionToFollyRange) {
static_assert(tmp.begin() == piece.begin(), "");
static_assert(tmp.end() == piece.end(), "");
}
#if FOLLY_HAS_STRING_VIEW
TEST(FixedStringConversionTest, ConversionToStringView) {
static constexpr folly::FixedString<16> tmp{"This is a string"};
constexpr std::string_view view = tmp;
static_assert(tmp.data() == view.data(), "");
static_assert(tmp.size() == view.size(), "");
}
#endif // FOLLY_HAS_STRING_VIEW
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment