|
Title: variable length argument lists Post by: Kekskiller on March 02, 2011, 02:09:15 PM So everyone familiar with the standard C library knows that printf etc use variable argument lists (or whatever you can call them). I haven't yet designed something using them, but I kind of like to do so - only that I wonder if there's a popular pitfall or so I didn't noticed in concept. Haven't seen many libraries or functions using them, stdarg.h itself is also no real help as it seems that it depends on the compiler for it's functionality.
So yeah, guess what my question is: Why aren't there millions of functions using it? Is it generally disliked or isn't it typesafe or what? I'd like to avoid stuff that's only making more problems. Title: Re: variable length argument lists Post by: Average Software on March 02, 2011, 02:50:01 PM Gtk has a fair number of functions that use them.
I think the reason you don't see them too often is because of the lack of type safety, and the difficulty the compiler has in recognizing errors. C++0x's variadic templates solve those problems for the most part. If you want to get really fancy, C99 has variadic macros, which I believe are being picked up in C++0x. Title: Re: variable length argument lists Post by: LemonScented on March 02, 2011, 02:58:44 PM This isn't an area of C I've looked into particularly deeply, and none of the references I have seem to explicitly list pros and cons with variable argument lists, so take all of this with a pinch of salt...
It would certainly seem that you can't guarantee any type safety with variable argument lists - even with printf you can pass it any old garbage to try to format. I could also believe that there might be some performance issues too, unless the compiler is doing something particularly special. The code to iterate through the arguments is a bit fiddly as well, I suppose, but nothing too horrible. I suspect the main reason they're not used is because there's not much call for them. I see them used fairly frequently in functions that work with strings like printf does (for assert/error messages, or for printing text onscreen via a font renderer, for example), but aside from that I've never seen a need for variable arguments anywhere else. Unless your compiler only supports C, you should be able to use C++ style default arguments, which can do a similar job of allowing you to be more flexible in the parameters you pass (or don't pass) to functions. Default arguments aren't as flexible as the varargs stuff, but they're pretty good and they're type-safe. Title: Re: variable length argument lists Post by: Kekskiller on March 02, 2011, 04:42:44 PM Hm. Seems ok then. Though you're right, LemonScented. I couldn't think of too many functions really needing it. But well, in GTK that may something different as it's a GUI and thus a lot more high level and flexibility-oriented, I think. Didn't yet made any GTK project, so I might look at it's references to what it's used for.
Though I can imagine it beeing interesting for general purpose merger functions for a given set of arrays, lists etc. Just something you'd of course also do with functions, but maybe quick to call with one instead of n-1 ones for mixing just two of them. Anyway, thanks for the info. |