Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

c++: undefined number of method arguments

19 views
Skip to first unread message

poor...@gmail.com

unread,
Feb 11, 2008, 4:29:08 PM2/11/08
to
Hello everyone!
Recently I came across a problem. I found myself wanting static method
of a class to have undefined number of arguments. I mean something
like C functions, i.e. printf, where u enter format string (the one
with %d, %c and so on), and then one additional argument for each
variable in format string. It is clear to me, that upon declaration of
printf function the exact number of arguments is unknown. I want my
function to behave in this exact way, that is to take on or more
arguments and then operate on them. I know that i can make function
have list as an argument, and create that list just before using the
function, but this is kinda inconvinient, as i intend to have all
functions (and ways to use them) as simple as possible.

How do i make something like this? Is this even doable?

rsal...@gmail.com

unread,
Feb 11, 2008, 5:14:19 PM2/11/08
to

google "va_list"

Brog

unread,
Feb 11, 2008, 5:18:05 PM2/11/08
to

poor...@gmail.com

unread,
Feb 11, 2008, 5:23:24 PM2/11/08
to
thank you very much, this is exactly what i was looking for

Martin Read

unread,
Feb 11, 2008, 5:42:49 PM2/11/08
to

It is. You want to #include <cstdarg> then declare your member function
as follows:

class Foo
{
// ...
public:
int my_variadic_function(const char *fmt, ...);
}

and in the function definition for Foo::my_variadic_function:

int Foo::my_variadic_function(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
// Use va_arg(ap, type) to get successive arguments, treating them
// as being of type 'type'.

// When you're done, clean up with va_end before returning; if you
// have multiple return points, *all* of them must call va_end.
va_end(ap);
return 0;
}

--
\_\/_/ some girls wander by mistake into the mess that scalpels make
\ / are you the teachers of my heart? we teach old hearts to break
\/ --- Leonard Cohen, "Teachers"

Gerry Quinn

unread,
Feb 12, 2008, 8:50:09 AM2/12/08
to
In article <cc86b9b6-47ba-4a51-bcec-
bace14...@e25g2000prg.googlegroups.com>, poor...@gmail.com says...

As has been stated in the thread, it is doable. Whether it's actually
simpler than using a list, I'm not sure...

- Gerry Quinn

Jeff Lait

unread,
Feb 12, 2008, 10:30:37 AM2/12/08
to
On Feb 11, 5:42 pm, Martin Read <mpr...@chiark.greenend.org.uk> wrote:

> poorch...@gmail.com wrote:
> >Hello everyone!
> >Recently I came across a problem. I found myself wanting static method
> >of a class to have undefined number of arguments. I mean something
> >like C functions, i.e. printf, where u enter format string (the one
> >with %d, %c and so on), and then one additional argument for each
> >variable in format string. It is clear to me, that upon declaration of
> >printf function the exact number of arguments is unknown. I want my
> >function to behave in this exact way, that is to take on or more
> >arguments and then operate on them. I know that i can make function
> >have list as an argument, and create that list just before using the
> >function, but this is kinda inconvinient, as i intend to have all
> >functions (and ways to use them) as simple as possible.
>
> >How do i make something like this? Is this even doable?
>
> It is. You want to #include <cstdarg> then declare your member function
> as follows:


Please note, however, that if you want to chain to another variadic
function, ie, vsprintf, you may need to use va_copy or risk crashes on
some platforms.
--
Jeff Lait
(POWDER: http://www.zincland.com/powder)

jot...@hotmail.com

unread,
Feb 12, 2008, 4:00:37 PM2/12/08
to
On 12 Fev, 15:30, Jeff Lait <torespondisfut...@hotmail.com> wrote:
>
> Please note, however, that if you want to chain to another variadic
> function, ie, vsprintf, you may need to use va_copy or risk crashes on
> some platforms.
> --
> Jeff Lait
> (POWDER:http://www.zincland.com/powder)

And if I recall correctly, the standard functions don't tell you when
the list ends, you have to figure that out yourself. Functions like
printf get that from the format string. Other alternatives are passing
the number of arguments before the actual list (might be a bit error-
prone) or having a list terminator element (don't forget it or this
thing will crash badly :) ). Another alternative is using the boost
preprocessor library, that has facilities to create multiple overloads
of the same function with different numbers of arguments, using some
preprocessor tricks. And finally, you can just overload the << or >>
operator to compose a list in a single line, like you do when you use
cout<<"hey"<<endl; (my favorite).

Jotaf

0 new messages