views:

66

answers:

3

Is it possible to have a function with variable arguments and no named argument?

For example:

SomeLogClass("Log Message Here %d").Log(5);
SomeLogClass("Log Message Here %d").Error(5);
+1  A: 

Can you write code like that above - yes you can. But you cannot portable write a variadic function without at least one non-variadic parameter. In printf(), for example, this is the format string. In other words, you can write function s like:

int printf( const char * format, ... );

but not:

int printf( ... );
anon
+1  A: 

Take a look at QString's arg methods. Those seem to be something you're looking for.

You can definitely roll your own, although implementation might turn out to be not really trivial, especially if you would like it to support printf format specifiers. If printf style is not necessary, chaining a replace_all kind of calls sounds doable.

Dmitry
Thanks. I didn't think of doing it that way.
Simie
A: 

Where I am from we use this:

Log << "LogMessageHere: " << ErrorClass << 5 << whatever << std::endl;

It is not exactly an answer to you question, but it is a solution to your problem, and I think it is more c++ like.

daramarak