Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ format macro / inline ostringstream

I'm trying to write a macro that would allow me to do something like: FORMAT(a << "b" << c << d), and the result would be a string -- the same as creating an ostringstream, inserting a...d, and returning .str(). Something like:

string f(){
   ostringstream o;
   o << a << "b" << c << d;
   return o.str()
}

Essentially, FORMAT(a << "b" << c << d) == f().

First, I tried:

1: #define FORMAT(items)                                                   \
   ((std::ostringstream&)(std::ostringstream() << items)).str()

If the very first item is a C string (const char *), it will print the address of the string in hex, and the next items will print fine. If the very first item is an std::string, it will fail to compile (no matching operator <<).

This:

2: #define FORMAT(items)                                                   \
   ((std::ostringstream&)(std::ostringstream() << 0 << '\b' << items)).str()

gives what seems like the right output, but the 0 and \b are present in the string of course.

The following seems to work, but compiles with warnings (taking address of temporary):

3: #define FORMAT(items)                                                   \
   ((std::ostringstream&)(*((std::ostream*)(&std::ostringstream())) << items)).str()

Does anyone know why 1 prints the address of the c-string and fails to compile with the std::string? Aren't 1 and 3 essentially the same?

I suspect that C++0x variadic templates will make format(a, "b", c, d) possible. But is there a way to solve this now?

like image 505
cadabra Avatar asked Nov 19 '08 22:11

cadabra


2 Answers

You've all pretty much nailed this already. But it's a little challenging to follow. So let me take a stab at summarizing what you've said...


That difficulties here are that:

  • We are playing with a temporary ostringstream object, so taking addresses is contra-indicated.

  • Because it's a temporary, we cannot trivially convert to an ostream object through casting.

  • Both the constructor [obviously] and str() are class ostringstream methods. (Yes, we need to use .str(). Using the ostringstream object directly would wind up invoking ios::operator void*(), returning a pointer-like good/bad value and not a string object.)

  • operator<<(...) exists as both inherited ostream methods and global functions. In all cases it returns an ostream& reference.

  • The choices here for ostringstream()<<"foo" are the inherited method ostream::operator<<(void* ) and the global function operator<<(ostream&,const char* ). The inherited ostream::operator<<(void* ) wins out because we can't convert to an ostream object reference to invoke the global function. [Kudos to coppro!]


So, to pull this off, we need to:

  • Allocate a temporary ostringstream.
  • Convert it to an ostream.
  • Append data.
  • Convert it back to an ostringstream.
  • And invoke str().

Allocating: ostringstream().

Converting: There are several choices. Others have suggested:

  • ostringstream() << std::string() // Kudos to *David Norman*
  • ostringstream() << std::dec // Kudos to *cadabra*

Or we could use:

  • ostringstream() . seekp( 0, ios_base::cur )
  • ostringstream() . write( "", 0 )
  • ostringstream() . flush()
  • ostringstream() << flush
  • ostringstream() << nounitbuf
  • ostringstream() << unitbuf
  • ostringstream() << noshowpos
  • Or any other standard manipulator. [#include <iomanip>] Reference: See "Insert data with format" 1/3 of the way down on this webpage.

We cannot use:

  • operator<<( ostringstream(), "" )
  • (ostream &) ostringstream()

Appending: Straightforward now.

Converting back: We could just use (ostringstream&). But a dynamic_cast would be safer. In the unlikely event dynamic_cast returned NULL (it shouldn't), the following .str() will trigger a coredump.

Invoking str(): Guess.


Putting it all together.

#define FORMAT(ITEMS)                                             \
  ( ( dynamic_cast<ostringstream &> (                             \
         ostringstream() . seekp( 0, ios_base::cur ) << ITEMS )   \
    ) . str() )

References:

  • IOstream Library
  • ostringstream
  • ostream::operator<<()

  • Type Casting Tutorial
  • Wiki: Type Casting

.

like image 111
Mr.Ree Avatar answered Sep 29 '22 16:09

Mr.Ree


Here is what I use. It all fits into one tidy class definition in a header file.

update: major improvement to the code thanks to litb.

// makestring.h:

class MakeString
{
    public:
        std::stringstream stream;
        operator std::string() const { return stream.str(); }

        template<class T>
        MakeString& operator<<(T const& VAR) { stream << VAR; return *this; }
};

Here is how it is used:

string myString = MakeString() << a << "b" << c << d;
like image 32
e.James Avatar answered Sep 29 '22 15:09

e.James