Abseil-equivalent of stringstream

21 views
Skip to first unread message

David Chen

unread,
Jun 20, 2024, 1:37:44 AMJun 20
to Abseil.io
Hi all,

What is the Abseil-equivalent of stringstream, in particular for appending characters to a string buffer? I thought that absl::StrAppend would be the right solution, but the char constructor for AlphaNum is explicitly deleted.

Thank you in advance for your help.

Best,
David

Rong Jie

unread,
Jun 20, 2024, 9:52:48 PMJun 20
to Abseil.io

Rong Jie

unread,
Jun 20, 2024, 10:13:51 PMJun 20
to Abseil.io
 Ah, my mistake, seems that absl::Cord also doesn't support explicitly appending char type.

There is kind of a workaround for both absl::Cord (more recommended) and absl::StrAppend if you just want to avoid malloc.

char raw[2] = {0};
s[0] = 'c';
abs::string_view s(raw);

then either absl::StrAppend(dest, s) or cord.Append(s)

Mark Barolak

unread,
Jun 22, 2024, 11:40:18 AMJun 22
to Rong Jie, Abseil.io
There is an internal type that does what you're asking for: https://github.com/abseil/abseil-cpp/blob/master/absl/strings/internal/ostringstream.h

There isn't a strong reason for it to be internal, but at the time of release there also wasn't a particularly strong reason for it to be public, so it was kept internal to be conservative.  I can ask a bit about making it public, but I should caution that it's not clear what the outcome of that should be.

Taking a step back: David, would you mind describing the problem you're trying to solve a bit more?  is there a reason why you'd like to avoid std::ostringstream?  Personally, I think I would reach for the standard library type without much second thought, so I'm really interested to learn more about why an Abseil type would be preferable.

--
You received this message because you are subscribed to the Google Groups "Abseil.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to abseil-io+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/abseil-io/dea7aba9-cfb3-4579-9c81-5e2686af11f5n%40googlegroups.com.

Andy Getzendanner

unread,
Jun 24, 2024, 1:50:38 PMJun 24
to David Chen, Abseil.io
On Thu, Jun 20, 2024 at 1:37 AM David Chen <david....@gmail.com> wrote:
I thought that absl::StrAppend would be the right solution, but the char constructor for AlphaNum is explicitly deleted.

You just need to convert that char to something else before you pass it into StrAppend (or StrCat).

If you want numerals, like "65", do absl::StrAppend(&my_str, static_cast<int>(my_char)).
If you want a single character, like "A", do absl::StrAppend(&my_str, absl::string_view(&my_char, 1)).

These functions reject char arguments because the intent is ambiguous, and we prefer for the code to be unambiguous even though that requires being more explicit in this case.
Reply all
Reply to author
Forward
0 new messages