Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type-safety by using the ellipsis notation

Tags:

c++

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

like image 996
sami Avatar asked Sep 02 '26 11:09

sami


1 Answers

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.

like image 74
Mike Seymour Avatar answered Sep 04 '26 00:09

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!