I've been adding std::string_views to some old code for representing string like config params, as it provides a read only view, which is faster due to no need for copying.
However, one cannot concatenate two string_view together as the operator+ isn't defined. I see this question has a couple answers stating its an oversight and there is a proposal in for adding that in. However, that is for adding a string and a string_view, presumably if that gets implemented, the resulting concatenation would be a std::string
Would adding two string_view also fall in the same category? And if not, why shouldn't adding two string_view be supported?
Sample
std::string_view s1{"concate"};
std::string_view s2{"nate"};
std::string_view s3{s1 + s2};
And here's the error
error: no match for 'operator+' (operand types are 'std::string_view' {aka 'std::basic_string_view<char>'} and 'std::string_view' {aka 'std::basic_string_view<char>'})
A view is similar to a span in that it does not own the data, as the name implies it is just a view of the data. To concatenate the string views you'd first need to construct a std::string then you can concatenate.
std::string s3 = std::string(s1) + std::string(s2);
Note that s3 will be a std::string not a std::string_view since it would own this data.
If you happen to already use abseil in your codebase there is also absl::StrCat which is good about avoiding too many intermediate std::string creations and can directly accept std::string_view arguments
std::string s3 = absl::StrCat(s1, s2);
A std::string_view is a lightweight, non-owning view of the characters.
To get a view that concatenates multiple string views, we can use the join view adapter that was introduced in C++20:
auto const joined = std::views::join(std::array{s1, s2});
This gives us a view object that can be iterated over using standard algorithms or range-based for. It can be converted to a std::string object (but not directly to a std::string_view as that requires us to copy the contents somewhere to make them contiguous).
Full demo:
#include <algorithm>
#include <array>
#include <ranges>
#include <string_view>
int main()
{
const std::string_view s1{"con"};
const std::string_view s2{"cate"};
const std::string_view s3{"nate"};
return !std::ranges::equal(std::views::join(std::array{s1, s2, s3}),
std::string_view{"concatenate"});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With