[lug] [OT] C++ trick?

Scott Herod herod at dimensional.com
Thu Jan 23 21:23:35 MST 2003


Hello,

I'm looking for C++ trick (mainly because it's pretty late, and I'm
getting tired of sending someone notes that the function he's using is
not in a standard header file).

I want to create a macro to replace "itoa( i, buf, size )" basically with
a function like:

string f ( int i ) {
  stringstream b;
  b << i << ends;
  return b.str();
}


The following doesn't work because the operator<<() creates ostreams which
don't know about the str() function of stringstreams.

#define itoa(i,buf,val) \
(stringstream().operator<<(i).operator<<(ends)).str()


Can someone suggest a trick?  I'm going to send the culprit a note asking
him to include my #define immediately after there #include <stdlib.h>.

Thanks

Scott





More information about the LUG mailing list