Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to creat function to call eg int function1(int, ...)

Reply
Thread Tools

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

 
 
Angus
Guest
Posts: n/a
 
      08-12-2011
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
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-12-2011
Angus <(E-Mail Removed)> writes:
>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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-12-2011
On 8/12/2011 2:58 PM, Angus wrote:
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-12-2011
On 12.08.2011 20:58, Angus wrote:
>
> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-14-2011
Victor Bazarov <(E-Mail Removed)> wrote:
> 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?
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      08-14-2011
Juha Nieminen <(E-Mail Removed)> writes:
>Victor Bazarov <(E-Mail Removed)> wrote:
>>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?


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«.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-15-2011
On 8/14/2011 2:18 PM, Juha Nieminen wrote:
> Victor Bazarov<(E-Mail Removed)> wrote:
>> 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?


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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-15-2011
Victor Bazarov <(E-Mail Removed)> wrote:
> On 8/14/2011 2:18 PM, Juha Nieminen wrote:
>> Victor Bazarov<(E-Mail Removed)> wrote:
>>> 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?

>
> 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.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      08-15-2011
On 15 Aug., 15:33, Juha Nieminen wrote:
>
> 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.

--
SG
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      08-15-2011
On Mon, 15 Aug 2011 13:33:45 +0000, Juha Nieminen wrote:

> 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why does "template<typename T> ... function<T(int)>" not match "int(&)(int)" implicitly? Steve Hicks C++ 2 09-28-2009 05:24 PM
How can I creat a time function or a class? learning_C++ C++ 2 09-23-2004 06:47 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments