Boost logo

Boost :

From: Stephan Menzel (smenzel_at_[hidden])
Date: 2007-07-31 16:34:48


Hi all,

I'd like to create a method using some sort of variable argument list like it
would be in printf style:

foo(int code, const char *fmt, ...) {}
...but using boost format.
Basically, the function shall compute the format string and than create a new
string with the number in "code" in front of it and the result of the
computed format following. It's for returning standard replies and do some
stuff depending on the error code. And it must be as performant as
reasonable.

I only came up with something like this:

void foo(int code, boost::format &fmt) {

        boost::format retfrmt("%d - message: ");
        retfrmt % code;
        std::cout << retfrmt.str().append(fmt.str()) << std::endl;
}

int main(int argc, char **argv) {

        reply(42, boost::format("%d test %s\n") % 23 % "Hello");
        return 0;
}

which does what I expected:

42 - message: 23 test Hello

But I don't like the usage a lot. Especially the fact that I have to write
boost::format(...) when using the function. (could use a macro so that's not
the killer issue) Also there seems to be a lot of unnecessary string copying
involved so I wonder if this is a good way to do this. Is there any better
solution to this?

Thanks and greetings...

Stephan




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