 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
peter_ammon@rocketmail.co Guest
|
Posted: Wed Apr 27, 2005 7:07 am Post subject: Variadic functions with no parameters |
|
|
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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Apr 27, 2005 2:24 pm Post subject: Re: Variadic functions with no parameters |
|
|
[email]peter_ammon (AT) rocketmail (DOT) com[/email] wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Wed Apr 27, 2005 10:06 pm Post subject: Re: Variadic functions with no parameters |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | peter_ammon (AT) rocketmail (DOT) 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.
|
Eh?
Isn't that what the macros va_list, va_start, va_arg, and va_end are for?
-Howard
|
|
| Back to top |
|
 |
Ken Human Guest
|
Posted: Wed Apr 27, 2005 10:25 pm Post subject: Re: Variadic functions with no parameters |
|
|
Howard wrote:
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:ngNbe.66305$NC6.31154 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
[email]peter_ammon (AT) rocketmail (DOT) com[/email] 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, ...);"
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Apr 27, 2005 10:25 pm Post subject: Re: Variadic functions with no parameters |
|
|
Howard wrote:
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:ngNbe.66305$NC6.31154 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
[email]peter_ammon (AT) rocketmail (DOT) com[/email] 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.
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Apr 28, 2005 2:02 pm Post subject: Re: Variadic functions with no parameters |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | Howard wrote:
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:ngNbe.66305$NC6.31154 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
[email]peter_ammon (AT) rocketmail (DOT) com[/email] 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
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Fri Apr 29, 2005 6:06 am Post subject: Re: Variadic functions with no parameters |
|
|
[email]peter_ammon (AT) rocketmail (DOT) com[/email] wrote:
| Quote: | The C++ grammar appears to admit (and g++ accepts)
void function(...);
|
Yes, it's allowed.
| Quote: |
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.
| Quote: | 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.
| Quote: | 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
|
|
| Back to top |
|
 |
Krzysztof Zelechowski Guest
|
Posted: Wed May 04, 2005 9:39 pm Post subject: Re: Variadic functions with no parameters |
|
|
Uzytkownik <peter_ammon (AT) rocketmail (DOT) com> napisal w wiadomosci
news:1114551200.309625.229930 (AT) z14g2000cwz (DOT) googlegroups.com...
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|