Variadic functions with no parameters

Discussion in 'C++' started by peter_ammon@rocketmail.com, Apr 27, 2005.

  1. Guest

    The C++ grammar appears to admit (and g++ accepts)

    void function(...);

    In such a function, how do you access any of the parameters? And what
    was the motivation for allowing functions of this type where C forbids
    them?

    I also notice that C++ makes the comma optional, whereas it's mandatory
    in C. That is, this is a legal prototype:

    void function2(int ...);

    What is the motivation for this change?

    Followups set to comp.lang.c++

    Thanks,
    -Peter

    ---
    [ comp.std.c++ is moderated. To submit articles, try just posting with ]
    [ your news-reader. If that fails, use mailto: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
    , Apr 27, 2005
    #1
    1. Advertising

  2. wrote:
    > The C++ grammar appears to admit (and g++ accepts)
    >
    > void function(...);
    >
    > In such a function, how do you access any of the parameters? [..]


    There is no way.
    Victor Bazarov, Apr 27, 2005
    #2
    1. Advertising

  3. Howard Guest

    "Victor Bazarov" <> wrote in message
    news:ngNbe.66305$01.us.to.verio.net...
    > wrote:
    >> The C++ grammar appears to admit (and g++ accepts)
    >>
    >> void function(...);
    >>
    >> In such a function, how do you access any of the parameters? [..]

    >
    > There is no way.


    Eh?

    Isn't that what the macros va_list, va_start, va_arg, and va_end are for?

    -Howard
    Howard, Apr 27, 2005
    #3
  4. Ken Human Guest

    Howard wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ngNbe.66305$01.us.to.verio.net...
    >
    >> wrote:
    >>
    >>>The C++ grammar appears to admit (and g++ accepts)
    >>>
    >>>void function(...);
    >>>
    >>>In such a function, how do you access any of the parameters? [..]

    >>
    >>There is no way.

    >
    >
    > Eh?
    >
    > Isn't that what the macros va_list, va_start, va_arg, and va_end are for?
    >
    > -Howard
    >
    >


    Yes, but va_start requires a pointer to the first argument in the list.
    The function's format must be similiar to "void function(T
    pFirstElement, ...);"
    Ken Human, Apr 27, 2005
    #4
  5. Howard wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ngNbe.66305$01.us.to.verio.net...
    >
    >> wrote:
    >>
    >>>The C++ grammar appears to admit (and g++ accepts)
    >>>
    >>>void function(...);
    >>>
    >>>In such a function, how do you access any of the parameters? [..]

    >>
    >>There is no way.

    >
    >
    > Eh?
    >
    > Isn't that what the macros va_list, va_start, va_arg, and va_end are for?


    Yes they are. Please explain how you'd use them in the case where
    there are no _named_ arguments.
    Victor Bazarov, Apr 27, 2005
    #5
  6. Howard Guest

    "Victor Bazarov" <> wrote in message
    news:DjUbe.66573$01.us.to.verio.net...
    > Howard wrote:
    >> "Victor Bazarov" <> wrote in message
    >> news:ngNbe.66305$01.us.to.verio.net...
    >>
    >>> wrote:
    >>>
    >>>>The C++ grammar appears to admit (and g++ accepts)
    >>>>
    >>>>void function(...);
    >>>>
    >>>>In such a function, how do you access any of the parameters? [..]
    >>>
    >>>There is no way.

    >>
    >>
    >> Eh?
    >>
    >> Isn't that what the macros va_list, va_start, va_arg, and va_end are for?

    >
    > Yes they are. Please explain how you'd use them in the case where
    > there are no _named_ arguments.


    Oh, I get your point now. I forgot that you need one named parameter to get
    started. Thanks.
    -Howard
    Howard, Apr 28, 2005
    #6
  7. wrote:

    > The C++ grammar appears to admit (and g++ accepts)
    >
    > void function(...);


    Yes, it's allowed.

    >
    > In such a function, how do you access any of the parameters?


    You cannot portably do so. It may be possible in
    implementation-specific ways.

    > And what
    > was the motivation for allowing functions of this type where C forbids
    > them?


    I don't know what the motivation was (apart from it being simpler
    than disallowing them), but this does have useful consequences in
    providing a worst-match for overload resolution, which can be
    useful in situations such as template metaprogramming.

    > I also notice that C++ makes the comma optional, whereas it's mandatory
    > in C. That is, this is a legal prototype:
    >
    > void function2(int ...);
    >
    > What is the motivation for this change?


    D&E might answer that, though I'm not sure if it does and don't
    have my copy handy right now to check.

    -- James
    James Dennett, Apr 29, 2005
    #7
  8. Uzytkownik <> napisal w wiadomosci
    news:...
    > The C++ grammar appears to admit (and g++ accepts)
    >
    > void function(...);
    >
    > In such a function, how do you access any of the parameters? And what
    > was the motivation for allowing functions of this type where C forbids
    > them?
    >
    >


    The reason is overloading. C language forbids overloaded functions, so
    there is no point of declaring formal parameters and not using them
    afterwards. You can just do without them.
    Chris
    Krzysztof Zelechowski, May 4, 2005
    #8
    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. Loony

    variadic functions

    Loony, Sep 12, 2003, in forum: C++
    Replies:
    2
    Views:
    363
    Kevin Goodsell
    Sep 12, 2003
  2. Colin Walters
    Replies:
    2
    Views:
    522
    Ben Pfaff
    Feb 13, 2004
  3. Ross A. Finlayson
    Replies:
    19
    Views:
    599
    Keith Thompson
    Mar 10, 2005
  4. Replies:
    2
    Views:
    346
    Dave Thompson
    Feb 27, 2006
  5. Replies:
    5
    Views:
    363
Loading...

Share This Page