147

Since C++17, we have std::string_view, a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use std::string_view.

However, one quickly finds out that switching from const std::string& to std::string_view breaks code that uses string concatenation as there is no support for concatenating std::string and std::string_view:

std::string{"abc"} + std::string_view{"def"}; // ill-formed (fails to compile)
std::string_view{"abc"} + std::string{"def"}; // ill-formed (fails to compile)

Why is there no support for concatenating std::string and std::string_view in the standard?

6
  • 17
    Most likely an oversight. That said, it doesn't take much to add a operator + to make the code work. Commented Jun 19, 2017 at 17:29
  • 9
    Just found this: groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/… Commented Jun 19, 2017 at 17:38
  • 3
    What I also miss is a member function of std::basic_string that returns the std::basic_string_view of a sub-string (similar to the "substr" member function), perhaps named "substr_view". Commented Jun 19, 2017 at 18:06
  • 1
    have a look at this. github.com/OlafvdSpek/xbt/blob/master/misc/xbt/string_view.h Commented Sep 27, 2017 at 17:12
  • 1
    @NathanOliver Doing that would conflict with a possible future addition of this operator to the language, so it is not recommended. Commented Jan 19, 2022 at 16:27

2 Answers 2

100

The reason for this is given in n3512 string_ref: a non-owning reference to a string, revision 2 by Jeffrey Yasskin:

I also omitted operator+(basic_string, basic_string_ref) because LLVM returns a lightweight object from this overload and only performs the concatenation lazily. If we define this overload, we'll have a hard time introducing that lightweight concatenation later.

It has been later suggested on the std-proposals mailing list to add these operator overloads to the standard. The paper to add them, P2591 Concatenation of strings and string views, has been approved for C++26.

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

14 Comments

Wow. Just wow! This seems to be the worst possible reason for leaving something out. The standard is supposed to be for the good of the language, not for any particular implementation. I'm also wary of the "performs the concatenation lazily" - does this mean that, should we have a memory shortage, the exception for adding to the original string will also be deferred? I sure as hell hope not, I want to know if an operation fails when I do it, not ten minutes later when I access the result.
Hopefully someone submit's a proposal to include these operators.
In the fmtlib/fmt library works great with string_view.
std::basic_string::append() has an overload that accepts everything that can be implicitly converted to a std::string_view. Of course this is not the same as operator+().
@joesdiner That is NOT safe, data is not null-terminated and const char*-taking overload expects it to be.
|
41

I've submitted P2591: Concatenation of strings and string views, linking to this SO question. The paper at this point it's targeted at C++26 minimum has been merged into C++26 (docs on cppreference).

5 Comments

This probably isn't where critique of the proposal should go, but I noticed that your example code has view as an std::string, not as an std::string_view.
Hi! Yes, duly noted; I've already fixed that and another couple of typos in an upcoming revision.
Will P2591 allow all of these? 1: sv + sv 2: sv + std::string 3: sv + string literal (const char*) 4: sv + character literal (char)
No, that's out of scope (in scope for a string builder, perhaps). Just kickstart the chain with a conversion to string, then keep chaining. (IOW, those opreations need to allocate memory; hence you need an allocator; but none of those operations let you specify one)
C++26?... Mind blown. IMHO this should be considered a defect and applied retroactively to C++17.

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.