C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Recursive Calls with Ellipses

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Erik Zawadzki
Guest





PostPosted: Sat Nov 13, 2004 2:39 am    Post subject: Recursive Calls with Ellipses Reply with quote



I'm trying to write a wrapper functions for a function that uses the
stdarg.h ellipses notation for variable arguments. In essense, if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but that lead
to some fun segmentation errors. Any other thoughts?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Sun Nov 14, 2004 5:00 am    Post subject: Re: Recursive Calls with Ellipses Reply with quote



Erik Zawadzki wrote:

Quote:
I'm trying to write a wrapper functions for a function that uses the
stdarg.h ellipses notation for variable arguments. In essense, if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but that lead
to some fun segmentation errors. Any other thoughts?

There is no portable way to wrap a function taking the ellipses argument.
To do that you need a function taking va_list instead of the ellipses.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Niklas Matthies
Guest





PostPosted: Sun Nov 14, 2004 5:03 am    Post subject: Re: Recursive Calls with Ellipses Reply with quote



On 2004-11-13 02:39, Erik Zawadzki wrote:
Quote:
I'm trying to write a wrapper functions for a function that uses the
stdarg.h ellipses notation for variable arguments. In essense, if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but that lead
to some fun segmentation errors. Any other thoughts?

It's not possible. If you are the author of foo(), you can rewrite it
to have a va_list parameter instead of the ellipsis. Then you can
write forwarding functions that themselves either have a va_list
parameter or an ellipsis. E.g.:

void vfoo(int n, va_list args);

void foo(int n, ...)
{
va_list args;
va_start(args, n);
vfoo(n, args);
va_end(args);
}

void vfoo_wrapper(int n, va_list args)
{
some_stuff();
vfoo(n, args);
some_other_stuff();
}

void foo_wrapper(int n, ...)
{
va_list args;
va_start(args, n);
vfoo_wrapper(n, args);
va_end(args);
}

For this reason it is good practice to always provide a corresponding
va_list version when providing a variadic function.

-- Niklas Matthies

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Seungbeom Kim
Guest





PostPosted: Sun Nov 14, 2004 10:43 am    Post subject: Re: Recursive Calls with Ellipses Reply with quote

Erik Zawadzki wrote:

Quote:
I'm trying to write a wrapper functions for a function that uses the
stdarg.h ellipses notation for variable arguments. In essense, if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but that lead
to some fun segmentation errors. Any other thoughts?

I'm afraid va_list is the only option that you can choose to pass a list
of variable arguments, and foo has to support it, as std::v*printf() do.

--
Seungbeom Kim

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Yechezkel Mett
Guest





PostPosted: Tue Nov 23, 2004 6:53 pm    Post subject: Re: Recursive Calls with Ellipses Reply with quote


"Erik Zawadzki" <epz (AT) interchange (DOT) ubc.ca> wrote

Quote:
I'm trying to write a wrapper functions for a function that
uses the
stdarg.h ellipses notation for variable arguments. In essense,
if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but
that lead
to some fun segmentation errors. Any other thoughts?

I can think of a number of possibilities.

1. Use a macro with a variable number of arguments. This is not
standard in C++03 but is in C99, so some compilers support it.

#define FOO_WRAPPER(n, ...) do {
..some stuff..
foo(n, __VA_ARGS__);
..some other stuff..
} while (false)

I hope I have the syntax correct - I copied it from the GCC docs.

2. Use a macro with 2 arguments, where the second argument is a
parenthesised list of the extra parameters.

To do this you will need some way of removing the parentheses
when calling the original foo. I don't know how to do this, but I
think the Boost MPL library has a method.

3. Use templates.

template <class Arg2Type>
void foo_wrapper(int n, Arg2Type arg2)
{
//some stuff
foo(n, arg2);
//some other stuff
}

template <class Arg2Type, class Arg3Type>
void foo_wrapper(int n, Arg2Type arg2, Arg3Type arg3)
{
//some stuff
foo(n, arg2, arg3);
//some other stuff
}

And so on, for as many arguments as you wish to support. I
believe Boost MPL can help you with the tedious work here as
well.

Yechezkel Mett



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron House
Guest





PostPosted: Wed Nov 24, 2004 9:33 am    Post subject: Re: Recursive Calls with Ellipses Reply with quote

Quote:
"Erik Zawadzki" <epz (AT) interchange (DOT) ubc.ca> wrote in message
news:cn3136$m82$4 (AT) nntp (DOT) itservices.ubc.ca...

I'm trying to write a wrapper functions for a function that

uses the

stdarg.h ellipses notation for variable arguments. In essense,

if the

original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but

that lead

to some fun segmentation errors. Any other thoughts?


Your problem is that you can't actually write out the text for whatever
was passed, because you only have access to a va_list. As foo is a fn
you wrote, you can 'back up' a bit and write a more generic version that
takes a va_list object. In other words, instead of trying to put foo
inside a wrapper, as you have done, make foo the wrapper outside of
something more useful. This is done with the printf family of functions,
and here is how to call such an one.

Here's a little wrapper function I once wrote to redirect and further
mangle sprintf output. prntbuf is a global buffer. Clearly there is a
big problem with this routine, but the bit you are asking about works fine.

int rhprintw(const char *format, ...) {
int result;
va_list ap;
va_start(ap, format);
result = vsprintf(prntbuf, (char*)format, ap);
putcrlf(prntbuf);
va_end(ap);
return result;
}

--
Ron House [email]house (AT) usq (DOT) edu.au[/email]
http://www.sci.usq.edu.au/staff/house

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bill Weston
Guest





PostPosted: Thu Nov 25, 2004 2:02 am    Post subject: Re: Recursive Calls with Ellipses Reply with quote

Erik Zawadzki <epz (AT) interchange (DOT) ubc.ca> wrote

Quote:
I'm trying to write a wrapper functions for a function that uses the
stdarg.h ellipses notation for variable arguments. In essense, if the
original function is:

void foo(int n, ...){
//body
}

I want to write a function like:

void foo_wrapper (int n, ...){
//some stuff
foo(n, ##SOMETHING##);
//some other stuff
}

Where the ##SOMETHING## is exactly what satisfied the "...".
How do I do this? I tried naively giving "foo" a va_list, but that lead
to some fun segmentation errors. Any other thoughts?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

what about overloading the first few numbers of arguments?

void foo_wrapper (int n){ foo(n); }

template< typename T1 >
void foo_wrapper (int n, T1 t1){ foo(n, t1);}

template< typename T1, typename T2 >
void foo_wrapper (int n, T1 t1, T2 t2){ foo(n, t1, t2);}

template< typename T1, typename T2 , typename T3 >
void foo_wrapper (int n, T1 t1, T2 t2, T3 t3){ foo(n, t1, t2, t3);}

template< typename T1, typename T2 , typename T3 , typename T4 >
void foo_wrapper (int n, T1 t1, T2 t2, T3 t3, T4 t4){ foo(n, t1, t2, t3, t4);}

etc.

Bill

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.