3

Since raw use of the iomanip stream modifiers is a) verbose and b) error prone (sticky vs. non-sticky, etc.), for user defined types, all that stuff can be hidden in the default operator<< ... as shown here, for example.

However, when formatting built-in types (even from within a user defined operator), it would be much more convenient (IMHO) to use an approach similar to std::quoted(beware C++14), where the data is wrapped in a function call that returns a temporary object that sets + resets the appropriate flags.

Essentially, instead of writing:

std::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n";

you would hypothetically write:

using namespace shorter;
std::cout << precis(2, fixed(1.234556)) << "\n";

As the example shows, combination of flags could get tricky and I'm sure the devil is in the details, so I was wondering whether there is any prior art / exiting helper library that tries to address "chevron hell". :-)

  • Is there any existing library addressing this?
  • What are the technical pitfalls here, if one wants to come up with a set of helpers oneself?

Specifically, this question is not about any kind of "type safe format string" (like Boost.Format offers).

I'm also not asking for a "best" library, I'm asking about any library doing this for normal ostreams. (Because I was not able to find any.)

For example, the write API of C++ Format does something like this, but it doesn't do it for std::iostreams, but for the library's "stream" type:

MemoryWriter out;
out << pad(hex(0xcafe), 8, '0');
Community
  • 1
  • 1
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • Boost.Format would probably be your friend but it's insanely slow. Just look up any other format library, there are many. Incidentally, you link to one in your question. – Rapptz Feb 09 '15 at 20:52
  • 1
    @MartinBa you'll need to sidestep the pedant crew by rephrasing your question to ask "how do I deal with these problems" instead of "is there a third party library that already deals with them". If there is such a library that someone knows about then they can bring it up. – M.M Feb 10 '15 at 00:41

0 Answers0