“I wanna do this, but typing a three line function to help is too much effort to bother.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14
std::string quote( const std::string& s )
{
std::ostringstream ss;
ss << std::quoted( s );
return ss.str();
}
std::string unquote( const std::string& s )
{
std::string result;
std::istringstream ss( s );
ss >> std::quoted( result );
return result;
}
Easy peasy.
1 2 3 4 5 6 7
std::string str1 = "quote me";
std::string str2;
str2 = "This should be quoted: " + quote(str1); // LOL, works!
str2 = quote(str1); // LOL, works!
str2 = unquote(str2); // hey, this works too! YAAAY!
The whole purpose of the streams library is to make stuff easy. If, by “simple task” you mean concatenation, then alas, quoting a string fails that criterion. Quoting a string is a rather involved task with a lot of edge cases that need to be properly handled. It is by no means a simple task.
The moral?
Don’t go throwing disdain around blithely.
Since there is function called std::quoted I thought that maybe it can do this.
I don't know what is so funny to you, you seem to laugh from your own jokes...
Anyway, I came here for help and if you want to reply in this tone, then don't reply at all. Don't waste your time cause I surely won't by reading it.
Yes, I know that, but if there is none in the middle it boils down to what I wrote. Not in terms of implementation (I've no idea how it is implemented) but the result. I just thought that since it takes string maybe it could also return a string.
Perhaps we should explain why it's silly to fret over this "overhead".
Chances are the actual I/O overhead of whatever is happening in the program here well outweighs any theoretical overhead that the additional stream operator has.
Premature optimization is the root of all evil -- Donald Knuth
If the scope of this is limited, and it's easier to write and maintain "\"" + string + "\"", then just do that and be done with it. But if you want more flexibility, use std::quoted. Don't be concerned about theoretical run-time "overhead", because chances are it doesn't make a perceivable difference.
C++ standard library's goal is to be supportive of general-use cases, so the std::quoted has the additional functionality of checking for inner quotes it needs to escape. I've never really had a use for it, but it's there...
What if a non-optimizing compiler decides to keep the function as a function? Then there would be overhead after compiling the program in that the function argument needs to also be read by std::quoted apart from just being sent to the stream.
LOL, I was reported for apologizing for being a jerk.
@Grime
The function is complex enough that it is unlikely to be inlined; it will always be a function. What Ganado is saying is that the function call and the work it does takes so little time from normal program processing that it is unlikely to be an issue.
Theoretical run time overhead is: function and work function does takes N amount of time, compared to simply concatenating three strings, which takes L amount of time, and nN > nL.
The reality is that actual run time overhead is: program spends n amount of time calling function, and T amount of time executing, and n << T*, so in relation to T, nL ≈ nN.
*Read as “significantly less than”, indicating a difference on an order of magnitude.
@Ganado you call it premature optimization and for me it is a common sense. If there is a way to avoid instantiation of additional object then I chose it. Of course if it doesn't hinder readability \ maintainability etc.
Right Duthomhas, so it would be unnecessary to call std::quoted, if it can be avoided ;/
Unspoken then would be right to say "too much overhead to accomplish such simple task", he meant that it was "unnecessary overhead" and you probably interpreted it differently.
See I'm not trying to be rude to you (for whatever reason you would think that), it's just that these are such small misunderstandings.
@Unspoken Just so you know, at least 2 new objects are created during the 2x + operators that the string concatenation version would do.
Of course if it doesn't hinder readability \ maintainability etc.
Good, that's what probably matters more.
What if a non-optimizing compiler decides to keep the function as a function?
(+) operators are functions too. Let's just paralyze ourselves and avoid factoring anything out into functions for fear of imaginary overhead.
Unspoken then would be right to say "too much overhead to accomplish such simple task
He would be right to say that once any actual profiling is done. Until this, I'm pretty confident in saying any overhead is like adding a gallon of water into the ocean, to put it less mathematically than Duthomhas.
The point I'm trying to make really has nothing to do with std::quoted in itself. I don't really give a hoot about that. What I'm trying to say is, many programmers fret over stuff that doesn't matter, I've seen it so many times, they get bogged down thinking about the minutia instead of just writing something. They worry about the fungus on one old tree instead of worrying about the health of the forest.
Write something, then profile it if you have good reason to think it might be the bottleneck of your program. Often times, what actually turns out to be the bottleneck can be surprising.
+1, I'd say the boilerplate code is the most annoying part, but makes it much cleaner and worth it, if you have to call that function from multiple places.