tags:

views:

100

answers:

4

Hi all,

it had been several times discussed in other subjects that it is not recommended to use variadic function because the compiler can't check the type of provided arguments.

But what about if the user knows exactly the type can be e.g. std::string. Could anything still go wrong here too?

regards

+4  A: 

The point is, even if you know what the types will be, one day, you'll pass the wrong type and the compiler won't tell you and you'll get a weird error.

If someone else joins the project, they may not be aware of any restrictions on the parameter types.

Also, if you're using an editor that does autocompletion / intellisense, it won't help you as it won't know the valid types.

Skizz
is there any restriction by using the data types. I mean, I have to use only the plain c/c++ data type or I can even use STL types, objects,...
sami
@sami: I'm not sure what you're trying to say there. There are no restrictions to parameter types that I'm aware of.
Skizz
@Skizz, @sami: there are restrictions; you can only pass "plain old data" (POD) types as ellipsis arguments. Certainly passing anything with a non-trivial destructor or copy constructor will give undefined behaviour.
Mike Seymour
@Mike Seymour: comment added below
sami
In addition, varargs functions promote some integer/float types to larger types. This is why `printf("%f", myFloat)` is okay, even though `%f` expects a `double`.
bk1e
A: 

If you know what type up front and templated version of your function should be able to give you the flexibility you need and guarantee type safety.

doron
+3  A: 

You may only pass "plain old data" (POD) types as variadic arguments. These are basic types (including pointers), and simple aggregates of other POD types; anything with non-trivial constructors, destructor, base classes or virtual functions are not POD. Passing a non-POD type gives undefined behaviour.

If you really want to use variadic functions in conjunction with complex types, you will have to pass them by pointer.

UPDATE: The C++0x draft relaxes "undefined behaviour" to "conditionally-supported, with implementation-defined semantics". I'm assuming this means you'll either get correct runtime behaviour (using the copy constructor/destructor where necessary), or a compile error, but never incorrect runtime behaviour from a conforming implementation.

Mike Seymour
@Mike Seymour: good point that I was not aware of. But to be more clearly. I am not allowed to pass an arbitrary number of objects as ellipsis arguments. is that right??Second, while the compiler does not take care about the the type of passed arguments, why should it claims by passing arguments of user defined type???
sami
@sami: The compiler doesn't have to (and probably won't) generate the code to correctly handle copying complex types; it will (most likely) do a simple memory copy. This means that you will get undefined behaviour; possibly it will work as you expect, possibly it will crash, possibly it will subtly leak memory or corrupt unrelated data, possibly you'll get a helpful compiler warning.
Mike Seymour
+2  A: 

As others have pointed out, ellipsis is C baggage and does not play nice with C++ concepts, such as non-trivial copy constructors.

C++0x has a safer alternative in more recent compilers (e.g. GCC 4.5), variadic templates.

Alex B
This had me confused now, one says that ellipsis arguments should only be plain old type, and you say that it could play nice even with c++...So, is it allowed to pass arbitrary objects of user defined typed or not?.
sami
@sami: No it's not allowed; I think this answer is supposed to read "does *not* play nice".
Mike Seymour
@sami: "So, is it allowed ...?" -- Not with a C-style ellipsis argument. And variadic templates is something very different. With variadic templates only the template has this "ellipsis" (argument pack). When a function is instantiated this gets replaced by the appropriate types *at compile-time*. So, type-safety is still present with variadic templates.
sellibitze
@sami @Mike @sellibitze Yes, it was a typo, good call.
Alex B