How to use std::quoted with string?

Hi,

I know that std::quoted is able write its output to the stream, but is it possible to use it with a string?

1
2
3
4
5
6
7
8
  #include <string>
  #include <iomanip>

  std::string str1 = "quote me";
  std::string str2;

  str2 = "This should be quoted: " + std::quoted(str1); //no operator "+" matches this operands.
  str2 = std::quoted(str1); //no operator "=" matches this operands. 


Thanks for your help.
Last edited on
Not directly, but it can be done using std::ostringstream.

1
2
3
std::ostringstream oss;
oss << std::quoted(str1);
std::string str2 = oss.str();
Thanks, I guess that is a too much overhead to accomplish such simple task.
Too much overhead? LOL.

“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.
Yes, for me instantiating std::ostringstream just to put a quotes around string is too much overhead. The fact that I need to do this is ridiculous.

I don't know about what edge cases you are talking about. I would simply like to have function which could realize this:

1
2
  std::string str1 = "quote me";
  str1 = "\"" + str1 + "\"";


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.

Last edited on
std::quoted doesn't just put quotes around the string. It also puts an escape character in front each quote characters in the string.

1
2
3
std::string str = "abc\"123";
std::cout << str; // abc"123
std::cout << std::quoted(str); // "abc\"123" 


Thanks for your input Peter.

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.

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.

Nah, you came here to snarf at reality.
I guess that is a too much overhead to accomplish such simple task.

If it doesn’t work the way you want it to, then it is beneath you.

I call BS.

Maybe you should consider your own attitude readjustment.
Since there is function called std::quoted I thought that maybe it can do this.

https://en.cppreference.com/w/cpp/io/manip/quoted
Returns an object of unspecified type such that the described behavior takes place.

The description mentions streams and only streams as is appropriate for I/O manipulators.
Hey, I came back to apologize. I’m out of line.

I think you’re being an idiot*, but I have no right to give you grief over it, or start a fight.

*IMNSHO
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...
Last edited on
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.

I'm legitimately asking.
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.

Hope this helps.
@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.
Last edited on
What is the actual "overhead" that is being discussed?

Is it "Oh no, the CPU cycles go to waste!"
or "Do I really have to type all that boiler-plate code?"

The first relates to profiling. The second to maintainable generic code.

If there are many strings to quote, then it makes sense to factor the operation within a function:
1
2
3
std::string quote( const std::string& );
std::string str1 = "quote me";
str1 = quote( str1 ); // frequent 

How you implement the quote() ... is your decision.
+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.
Last edited on
If P1479 gets accepted you'll be able to write std::to_string(std::quoted(str1)).

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1479r0.html
Topic archived. No new replies allowed.