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

Discussion in 'C++' started by Angus, Aug 12, 2011.

  1. Angus

    Angus Guest

    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
     
    Angus, Aug 12, 2011
    #1
    1. Advertising

  2. Angus

    Stefan Ram Guest

    Angus <> 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
     
    Stefan Ram, Aug 12, 2011
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Aug 12, 2011
    #3
  4. 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
     
    Alf P. Steinbach, Aug 12, 2011
    #4
  5. Victor Bazarov <> 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?
     
    Juha Nieminen, Aug 14, 2011
    #5
  6. Angus

    Stefan Ram Guest

    Juha Nieminen <> writes:
    >Victor Bazarov <> 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«.
     
    Stefan Ram, Aug 14, 2011
    #6
  7. On 8/14/2011 2:18 PM, Juha Nieminen wrote:
    > Victor Bazarov<> 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
     
    Victor Bazarov, Aug 15, 2011
    #7
  8. Victor Bazarov <> wrote:
    > On 8/14/2011 2:18 PM, Juha Nieminen wrote:
    >> Victor Bazarov<> 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.
     
    Juha Nieminen, Aug 15, 2011
    #8
  9. Angus

    SG Guest

    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
     
    SG, Aug 15, 2011
    #9
  10. Angus

    Nobody Guest

    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).
     
    Nobody, Aug 15, 2011
    #10
  11. Nobody <> wrote:
    > 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).


    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.
     
    Juha Nieminen, Aug 16, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CM
    Replies:
    3
    Views:
    1,787
  2. John

    Can creat asp.net apps

    John, Nov 6, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    418
    Daniel Rimmelzwaan
    Nov 7, 2003
  3. learning_C++
    Replies:
    2
    Views:
    601
    John Harrison
    Sep 23, 2004
  4. Schnoffos
    Replies:
    2
    Views:
    1,252
    Martien Verbruggen
    Jun 27, 2003
  5. Hal Styli
    Replies:
    14
    Views:
    1,712
    Old Wolf
    Jan 20, 2004
Loading...

Share This Page