When ... argument should be used?

P

Peng Yu

I searched boost and C++ standard library. I don't see an example that
defines a function with '...' as the argument, which will be used by
end users (like in printf). I'm wondering what is the general
guideline in use '...' as the function argument.
 
J

Jonathan Lee

I'm wondering what is the general
guideline in use '...' as the function argument.

My own guideline would be "don't do it". I can't speak
about the history of the <cstdarg> functions, but I would
guess they're obsolete in the face of passing classes,
overloading operators, etc. Probably for every use of it,
there would be a "more C++" way of doing the same thing.

I've personally never encountered a use for it.

--Jonathan
 
V

Victor Bazarov

Jonathan said:
My own guideline would be "don't do it". I can't speak
about the history of the <cstdarg> functions, but I would
guess they're obsolete in the face of passing classes,
overloading operators, etc. Probably for every use of it,
there would be a "more C++" way of doing the same thing.

I've personally never encountered a use for it.

Aside from the need to reuse the output mechanism provided by 'printf'
(which is, after all, quite convenient if you think about it), there has
never been other serious need in the ellipsis in all my years either.

V
 
P

Peng Yu

Aside from the need to reuse the output mechanism provided by 'printf'
(which is, after all, quite convenient if you think about it), there has
never been other serious need in the ellipsis in all my years either.

This prompts me the following questions:

1. What is the original purpose of having ... argument in C
2. For what disadvantages, ... argument become deprecated in C++?
3. What language construct in C++ are used to fulfill the original
purpose of ... argument in C?

I have been using C++ for a long time. It is natural for me not to
use ... argument.

However, '...' is extensively used in R (www.r-project.org), which is
the language that I am learning now. The usage of '...' in R is
similar to the usage of '...' in C, but not exactly. The fact
intrigues me that the similar language construct (i.e., '...') gets
deprecated in one language but gets widespread in another. Is there a
summary on the pros and cons of using '...' (at least in C++)?
 
I

Ian Collins

Peng said:
This prompts me the following questions:

1. What is the original purpose of having ... argument in C

Variadic functions are used where the number and/or types of the
function's parameters are unknown at compile time.
2. For what disadvantages, ... argument become deprecated in C++?

C++ provides function (and operator) overloading, default parameters and
templates. One or more of these can be used to provide a type safe
alternative in many of the places variadic functions are used in C.
Consider the difference between the printf family of functions and ostreams.
3. What language construct in C++ are used to fulfill the original
purpose of ... argument in C?

See above.
 
P

Peng Yu

To be exact, this is the compile time of the variadic function itself,
like printf() in the C runtime library.

On the other hand, every time one calls printf(), the compiler exactly
knows the number and types of the parameters you are passing.

Variadic functions are used to mimick function overloading in C. It is in
some sense more powerful as C++ function overloading as it handles well
the exponential explosion of argument type combinations. For implementing
something similar in C++ one needs to process arguments one-by-one, by
using overloaded functions repeatedly, or maybe one can use some heavy
template trickery instead.




Type safety. The arguments passed via ellipses totally bypass the
language type system and rely only on the user code to avoid UB. This can
be easily broken, e.g. when size_t changes from 32 to 64 bits.

As a consequence, no attempt was made to support proper C++ objects (non-
POD-s) in variadic functions, as the feature was totally broken anyway.
Thus in C++ the feature is also incomplete in that one cannot pass non-
POD objects.

If the R language manages to avoid the type safety and incompleteness
problems, then the feature is fine. If the object types are known at run-
time, it would be easy to check them or apply needed conversions.

I don't think that the R language manages to avoid the type safety and
incompleteness problems. According to my limited knowledge in R, R is
more similar to python in the sense that if the users what to check
the type they can do it but it is not mandatory.

As mentioned in http://bytes.com/topic/python/answers/676651-function-parameter-type-safety,
"Long story short: checking parameter types is un-Pythonic. Don't do
it."

Although type check is a strong point of C++, it not necessary widely
used in other languages. I want to understand the principle in
different aspects (include type safety). I checked the book
Programming Language Pragmatics by Michael L. Scott (2nd edition).
This is a pretty thick book. Could somebody let know if this book
would help me on understanding the different choices made in different
languages? If yes, what particular part of the book might help me
understand the reasons of the choices (in particular, the difference
between type safety, in C++, python, perl, R)?
 
J

James Kanze

This prompts me the following questions:
1. What is the original purpose of having ... argument in C

Support for things like printf and execl.
2. For what disadvantages, ... argument become deprecated in C++?

Can't interface some Posix system functions. (In other words,
it ain't gonna happen.)
3. What language construct in C++ are used to fulfill the
original purpose of ... argument in C?

The usual solution is to provide some sort of collector class,
and use it as an argument. So you could write something like:

f( collector.with(a).with(b).with(c) );
I have been using C++ for a long time. It is natural for me
not to use ... argument.
However, '...' is extensively used in R (www.r-project.org),
which is the language that I am learning now. The usage of
'...' in R is similar to the usage of '...' in C, but not
exactly. The fact intrigues me that the similar language
construct (i.e., '...') gets deprecated in one language but
gets widespread in another. Is there a summary on the pros and
cons of using '...' (at least in C++)?

Pros: none that I know of.
Cons: no type safety (even at runtime).
 

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
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top