views:

82

answers:

3

In my current project there's some variadic functions being used (ellipsis) that are really being used quite ofen. I have to make some impacting changes to the project, so I thought i might as well get rid of these variadics too.

The question I have is, how does one best replace these if the number of arguments passed to these are so variable that creating overloads would be silly?

I've heard about variadic templates, but are they a good replacement?

Most of the variadics used in my project are wrappers around sprintf or some such function.

A: 
  • Take an approach like iostreams do with some sort of operator/function to add items one at a time.
  • Accept a vector of items (if they're just the same type).
  • Create a class that wraps the needed parameter info and pass just an instance of that class (it could have multiple attributes each of which could have varying length if needed).
Mark B
+2  A: 

Depending upon how ingrained the variadics are, you might be on the losing end of the cost/benefit scale. Maintenance on large projects is always a balancing act between working with the code as it exists and what you Know Is Better. Be sure that the effort you put in gives real value in the result.

So with that caveat. Since you mention that your functions are built around sprintf, you might look into the C++ stream classes. You can consider std::ostringstream as a replacement for sprintf. A lexical cast like that provided by Boost may also help.

gregg
boost::format may also be useful
jk