Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string + string_view operators #1

Open
OlafvdSpek opened this issue Jan 13, 2017 · 5 comments
Open

string + string_view operators #1

OlafvdSpek opened this issue Jan 13, 2017 · 5 comments

Comments

@OlafvdSpek
Copy link

OlafvdSpek commented Jan 13, 2017

It'd be nice if the following (and more) operators were supported in Boost for std::string, boost::string_ref and boost::string_view. Since the standard doesn't provide them (yet) for std::string_view having them for that type would be great as well.

s += sv;
s + sv;
sv + s;
sv + sv; ?

@mclow
Copy link
Owner

mclow commented Jan 17, 2017

Let's talk about what these might do:
s += sv --> append the contents of sv to the string s.
s + sv --> create a new string with the contents of sv appended to the contents of s.
sv + s --> create a new string with the contents of s appended to the contents of sv.
sv1 + sv2 --> create a new string with the contents of sv1 appended to the contents of sv2.

I think that the last three are unwelcome. Silently creating a string when you're manipulating string_views is not (IMHO) a good idea.

@OlafvdSpek
Copy link
Author

OlafvdSpek commented Jan 17, 2017

Why?
AFAIK string_view is supposed to be a drop-in replacement for string in most cases, hence all member functions..
What would be the problem with s + sv?
string + const char* works fine.

@OlafvdSpek
Copy link
Author

s << s and s << sv would be nice too. They'd not create new strings and look similar to stream output operations.

@mclow
Copy link
Owner

mclow commented Jan 26, 2017

I'm not sure what you mean by s << s or s << sv. What operation would happen here?
I think you're right about s + sv, though.

@OlafvdSpek
Copy link
Author

s += s and s += sv, respectively. Like stringstream but using string instead.

It'd allow for a more efficient way to do:

string s;
s += A + B + to_string(42) + C; // mind the temporaries
s << A << B << to_string(42) << C;

s << A << B << 42 << C; // maybe even support integers and floats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants