Boost logo

Boost :

From: Scott Woods (scottw_at_[hidden])
Date: 2003-10-02 16:49:10


> template <typename T>
> string toString(T value, std::ios_base& (*manipulator)(std::ios_base&) )
> { // Convert T to string, using parameterless manipulator like std::hex.
> // Note uses default stream values, unless overwritten by manipulator.
> ostringstream oss;
> oss << manipulator << value; // No << ends required (causes a space and
test
> failure).
> cout << "value|" << value << "|" << ", toString |" << oss.str() << "|" <<
> endl; // for test only.
>
> return oss.str();
> }
>
> works as expected:
>

> But I am now stuck trying to test setw and setprecision with, for example,
> toString(15, setw(10))
>
> at first expecting
>
> string toString(T value, std::ios_base&
(*manipulator(int))(std::ios_base&) )
>
> or should it be
>
> string toString(T value, ) ?
>
> to do the business,
>
> but for these the compiler reports it is trying to match
>
> (int, std::_Smanip<_Arg>)
>
> (My fancy manipulators with an int parameter don't use smanip, so will I
have to
> provide another version using std::ios_base&
(*manipulator)(int)(std::ios_base&)
> ?)
>
> Further IOS expert suggestions most welcome.

Hmmm. There are a few things you got goin' on but will have a go at
responding to most immediate;

Looking at the #define from iomanip.h;

SMANIP(T)

i'm guessing that you want to add a function
something like;

template <typename T>
string toString( T value, SMANIP(int) m )

i.e. a template function that instantiates an overload. the
new arg being the type returned by a call to (for example)
setw(). starts to look like a small family of "toString"
functions is required. did a few syntax checks but not
really sure of your goals - don't know whether this is
helping or doing something more sinister. Its possible
that this is not boosting either :-)

Good luck.

SW


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk