Przemysław Czechowski
Przemysław Czechowski

Reputation: 776

How to convert boost::string_view to std::string_view?

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.

Upvotes: 5

Views: 6554

Answers (2)

IMelker
IMelker

Reputation: 65

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

Upvotes: 5

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136228

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.

Upvotes: 9

Related Questions