Reputation: 776
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
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
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