5

I'm using a boost library which uses boost::string_view. However, I would like to use std::string_view in my code.
Q: What's the best way to convert between these two?

At the moment I'm using:

void foo(std::string_view sv) {
# ...
}
void foo(boost::string_view bsv) {
  foo(std::string(bsv));
}

But this creates an unnecessary string.

2 Answers 2

9

One way:

void foo(std::string_view sv);

inline void foo(boost::string_view bsv) {
  foo(std::string_view(bsv.data(), bsv.size()));
}

Make sure to pass the length into std::string_view otherwise it calls Traits::length (std::strlen) unnecessarily.

Sign up to request clarification or add additional context in comments.

Comments

5

Or you can switch beast using it's own string_view to std::string_view, by providing definition of BOOST_BEAST_USE_STD_STRING_VIEW

1 Comment

Since boost-1.83.0 BOOST_BEAST_USE_STD_STRING_VIEW is no longer available: boost.org/doc/libs/1_83_0/libs/beast/doc/html/beast/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.