How to creat function to call eg int function1(int, ...)

A

Angus

Hi

I need to put all functions calling into a C API into a separate
source file unit. It is due to defines interfering with other code.
So to keep the possibly interfering code separate.

Anyway, most functions are eg int function2(int) so I just implement
in separate file like this:
int XXfunction1(int val) { return function(val) }

But I also have functions with variable arguments like this: extern
int function2(int, ...)

So how can I write my own function which calls that?

This doesn't seem to work:
int XXFunction2(int val, ...) {
return function2(val, ...);
}

How do I write the function?

Angus
 
S

Stefan Ram

Angus said:
This doesn't seem to work:
int XXFunction2(int val, ...) {
return function2(val, ...);
}

What you are looking for is »wrapping« or »perfect
forwarding« of a function that takes a variable number of
arguments. This is not possible AFAIK. Therefore, whenever a
party implements a variadic function, it also should
implement a function that takes a va_list argument, such as
for example: sprintf and vsprintf.

Possibly http://en.wikipedia.org/wiki/Variadic_templates might
help to do this in the upcoming C++ standard?

»I'd spell creat with an e.«

Ken Thompson, quoted via

http://en.wikiquote.org/wiki/Kenneth_Thompson
 
V

Victor Bazarov

I need to put all functions calling into a C API into a separate
source file unit. It is due to defines interfering with other code.
So to keep the possibly interfering code separate.

Anyway, most functions are eg int function2(int) so I just implement
in separate file like this:
int XXfunction1(int val) { return function(val) } ;

But I also have functions with variable arguments like this: extern
int function2(int, ...)

So how can I write my own function which calls that?

This doesn't seem to work:
int XXFunction2(int val, ...) {
return function2(val, ...);
}

How do I write the function?

There is no portable way. Some compilers would let you call that
function with 'va_list' instead of the arguments. Consult your compiler
documentation or post to the newsgroup for your compiler.

V
 
A

Alf P. Steinbach

I need to put all functions calling into a C API into a separate
source file unit. It is due to defines interfering with other code.
So to keep the possibly interfering code separate.

Anyway, most functions are eg int function2(int) so I just implement
in separate file like this:
int XXfunction1(int val) { return function(val) }

But I also have functions with variable arguments like this: extern
int function2(int, ...)

So how can I write my own function which calls that?

This doesn't seem to work:
int XXFunction2(int val, ...) {
return function2(val, ...);
}

How do I write the function?

In some cases, such as with printf family, you can find variants that
accept va_list. Then just use the standard library's macros.

Otherwise, you need to find a way to express a general call as a
sequence of calls with known arguments. That depends entirely on the
function. Maybe you need to single out the important use-cases.

The way standard iostreams do this can serve as main example of a
reasonable way to do it, but iostreams rely on the fact that arguments
are processed in sequence, which your function may not necessarily do.


Cheers & hth.,

- Alf
 
J

Juha Nieminen

Victor Bazarov said:
There is no portable way. Some compilers would let you call that
function with 'va_list' instead of the arguments. Consult your compiler
documentation or post to the newsgroup for your compiler.

Since when is the C89 standard (and hence by extension the C++98 standard)
not portable?
 
S

Stefan Ram

Juha Nieminen said:
Since when is the C89 standard (and hence by extension the
C++98 standard) not portable?

Here is a forwarder using vsprintf (untested):

/* ISO 8859-1, ISO/IEC 9899:1999 (E) */
#include <stdarg.h>
#include <stdio.h>

(...)

void xsprintf( char * const buf, const char * const fmt, ... )
{ va_list ap;
va_start( ap, fmt );
vsprintf( buf, fmt, ap );
va_end( ap ); }

I am not aware how to write this in a portable manner
using »sprintf« instead of »vsprintf«.
 
V

Victor Bazarov

Since when is the C89 standard (and hence by extension the C++98 standard)
not portable?

WTF are you talking about? I, for one, am tired of your riddles. Be
verbose. I have no intention of guessing what you mean.

V
 
J

Juha Nieminen

Victor Bazarov said:
WTF are you talking about? I, for one, am tired of your riddles. Be
verbose. I have no intention of guessing what you mean.

va_list is part of the C89 standard, yet you claim there is no portable
way.
 
S

SG

va_list is part of the C89 standard, yet you claim there is
no portable way.

I fail to see a contradiction here.
Maybe you did not understand what this thread is about.
 
N

Nobody

va_list is part of the C89 standard, yet you claim there is no portable
way.

Read the original question:
But I also have functions with variable arguments like this:
extern int function2(int, ...)

So how can I write my own function which calls that?

To which, the answer is "there is no portable solution". If the OP was
writing the function, he could just write a va_list based version. But if
all he has is a variadic function, there's no portable way to "wrap" it
(i.e. write a function which is itself variadic and which passes those
arguments down to the original function).
 
J

Juha Nieminen

Nobody said:
Read the original question:


To which, the answer is "there is no portable solution". If the OP was
writing the function, he could just write a va_list based version. But if
all he has is a variadic function, there's no portable way to "wrap" it
(i.e. write a function which is itself variadic and which passes those
arguments down to the original function).

I see. I thought the original question was how to pass a variable number
of arguments from one function (taking them) to another (also taking them),
for which there is indeed a standard solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top