Variadic functions with no parameters

P

peter_ammon

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
 
V

Victor Bazarov

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

Howard

Victor Bazarov said:
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
 
K

Ken Human

Howard said:
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, ...);"
 
H

Howard

Victor Bazarov said:
Howard said:
(e-mail address removed) 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
 
J

James Dennett

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
 
K

Krzysztof Zelechowski

Uzytkownik said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top