Connecting Tech Pros Worldwide Forums | Help | Site Map

Variadic functions with no parameters

peter_ammon@rocketmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Variadic functions with no parameters


peter_ammon@rocketmail.com wrote:[color=blue]
> The C++ grammar appears to admit (and g++ accepts)
>
> void function(...);
>
> In such a function, how do you access any of the parameters? [..][/color]

There is no way.
Howard
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Variadic functions with no parameters



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:ngNbe.66305$NC6.31154@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> peter_ammon@rocketmail.com wrote:[color=green]
>> The C++ grammar appears to admit (and g++ accepts)
>>
>> void function(...);
>>
>> In such a function, how do you access any of the parameters? [..][/color]
>
> There is no way.[/color]

Eh?

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

-Howard


Ken Human
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Variadic functions with no parameters


Howard wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:ngNbe.66305$NC6.31154@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>peter_ammon@rocketmail.com wrote:
>>[color=darkred]
>>>The C++ grammar appears to admit (and g++ accepts)
>>>
>>>void function(...);
>>>
>>>In such a function, how do you access any of the parameters? [..][/color]
>>
>>There is no way.[/color]
>
>
> Eh?
>
> Isn't that what the macros va_list, va_start, va_arg, and va_end are for?
>
> -Howard
>
>[/color]

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, ...);"
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Variadic functions with no parameters


Howard wrote:[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:ngNbe.66305$NC6.31154@newsread1.mlpsca01.us.t o.verio.net...
>[color=green]
>>peter_ammon@rocketmail.com wrote:
>>[color=darkred]
>>>The C++ grammar appears to admit (and g++ accepts)
>>>
>>>void function(...);
>>>
>>>In such a function, how do you access any of the parameters? [..][/color]
>>
>>There is no way.[/color]
>
>
> Eh?
>
> Isn't that what the macros va_list, va_start, va_arg, and va_end are for?[/color]

Yes they are. Please explain how you'd use them in the case where
there are no _named_ arguments.
Howard
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Variadic functions with no parameters



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:DjUbe.66573$NC6.37037@newsread1.mlpsca01.us.t o.verio.net...[color=blue]
> Howard wrote:[color=green]
>> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
>> news:ngNbe.66305$NC6.31154@newsread1.mlpsca01.us.t o.verio.net...
>>[color=darkred]
>>>peter_ammon@rocketmail.com 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.[/color]
>>
>>
>> Eh?
>>
>> Isn't that what the macros va_list, va_start, va_arg, and va_end are for?[/color]
>
> Yes they are. Please explain how you'd use them in the case where
> there are no _named_ arguments.[/color]

Oh, I get your point now. I forgot that you need one named parameter to get
started. Thanks.
-Howard




James Dennett
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Variadic functions with no parameters


peter_ammon@rocketmail.com wrote:
[color=blue]
> The C++ grammar appears to admit (and g++ accepts)
>
> void function(...);[/color]

Yes, it's allowed.
[color=blue]
>
> In such a function, how do you access any of the parameters?[/color]

You cannot portably do so. It may be possible in
implementation-specific ways.
[color=blue]
> And what
> was the motivation for allowing functions of this type where C forbids
> them?[/color]

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.
[color=blue]
> 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?[/color]

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
Krzysztof Zelechowski
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Variadic functions with no parameters



Uzytkownik <peter_ammon@rocketmail.com> napisal w wiadomosci
news:1114551200.309625.229930@z14g2000cwz.googlegr oups.com...[color=blue]
> 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?
>
>[/color]

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


Closed Thread