[PATCH] D132747: [llvm][ADT] Overload output stream operator `<<` for `StringMapEntry` and `StringMap`.

Dmitri Gribenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 29 05:34:23 PDT 2022


gribozavr2 added inline comments.


================
Comment at: llvm/include/llvm/ADT/StringMapEntry.h:166
+  return OS << "}";
+}
+
----------------
wyt wrote:
> gribozavr2 wrote:
> > I wonder if we should put these operators into llvm/Testing/ADT, since these operators are only supposed to be used from tests. For production code, LLVM strongly prefers llvm::raw_ostream, a much more light-weight type than std::ostream.
> > 
> > In fact, this is the first `operator<<(std::ostream&, <anything>)` overload in llvm/ADT. Printable types (like APInt) are printable into raw_ostream.
> > 
> > Another option is to provide `operator<<(llvm::raw_ostream&)` in llvm/ADT, and wrap them in `operator<<(std::ostream&)` in llvm/Testing/ADT. The llvm/ADT version won't have the SFINAE magic, but the llvm/Testing/ADT one will.
> > 
> > WDYT?
> Moved the printing into Testing/ADT.
> 
> I also attempted the second approach but it wasn't very successful. Using <<(llvm::raw_ostream) from <<(std::ostream) meant that <<(llvm::raw_ostream) needed to be implemented for any structure we wanted to print. However for use cases related to google test, it is more likely that users implement the <<(std::ostream) operator and not the <<(llvm::raw_ostream).
> Using <<(llvm::raw_ostream) from <<(std::ostream) meant that <<(llvm::raw_ostream) needed to be implemented

What I meant is that when implementing <<(std::ostream) you can call <<(llvm::raw_ostream) printing into a string, and then print the string into the std::ostream.

But that's not necessary, what you have now also works.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132747/new/

https://reviews.llvm.org/D132747



More information about the llvm-commits mailing list