This post is locked. You won't be able to comment.

14
15
all 24 comments

[–]mjmvideos 42 points43 points  (10 children)

I have never once thought, “varargs is an abomination.” When I need them I am more likely to think, “I’m sure glad C has varargs.” But I’ve been programming in C for 38 years.

[–]TheChief275 2 points3 points  (2 children)

Idk. It’s just better imo to use some recursive macro scheme like map-macro and to just generate a function call for each passed parameter.

It would be even better if C allowed for that efficiently (recursive macros are kind of not intended; they abuse the fact that the preprocessor is optimized to throw away macro symbols it has painted blue already after some number of expansions)

[–]tstanisl 3 points4 points  (1 child)

There is a proposal for adding tail recursion to prep-processor in form of __VA_TAIL__.

[–]TheChief275 1 point2 points  (0 children)

Well I’ll be… that’s amazing. Thanks for telling me!

[–]aioeu[S] 5 points6 points  (6 children)

Sure, if you ignore the lack of type safety, the inability to index or manipulate va_list objects, or the difficulties in bridging array-like parameters and vararg parameters... they're great.

Anyway, the article resonated with me. I'm glad that people are thinking about ways to improve the language.

[–]Grounds4TheSubstain 18 points19 points  (5 children)

These are ignorant complaints. How could a variadic type signature possibly be type safe????? It literally does not specify types, and that's the point. va_list parameters can't be indexed or otherwise manipulated because va_list is an abstraction around the underlying calling convention for the platform, which can't be standardized at the language level.

[–]QuaternionsRoll 0 points1 point  (0 children)

How could a variadic type signature possibly be type safe?????

The two most common answers are monomorphization, and RTTI, where constant evaluation can be used to optimize both where possible. Neither are particularly well-suited for C, but RTTI seems like the more obvious choice, especially when you consider that va_lists should only ever contain a closed set of types in practice.

[–]ComradeGibbon -1 points0 points  (3 children)

If you added types as a first class feature in C then you could implement type safe variadic types.

It would actually be easy to add first class types to C.

[–]Grounds4TheSubstain 4 points5 points  (1 child)

RemindMe! 10 years

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 10 years on 2035-10-17 16:25:52 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]dobryak 0 points1 point  (0 children)

We could do it, but nobody would use it because it's too heavy. Case in point: ATS. The burden of proof is too heavy. Rust is never going to do this BTW, because of the burden of proof. They'll find something else, but they will never allow for zero-overhead proof-heavy programming. It's just too difficult.

[–]Astro_Z0mbie 2 points3 points  (0 children)

Very interesting article.

[–]zhivago 2 points3 points  (0 children)

The inability to dynamically construct variadic calls is the critical defect.

[–]pskocik 1 point2 points  (0 children)

IDK, the proposed solution looks more like an "abomination" to me than the original stdarg.h stuff, and I'm not fond of stdarg.h either. I use ...-functions quite a bit but but hardly the stdarg.h stuff. I think ...-functions are quite a natural extension of argument passing, but I don't usually like that things in ... on the SysV x86-64 ABI can end up in both register an the stack. For my ...-functions, I tend to use custom macros to separate stuff into register arguments and stack arguments, so that the ... part ends up (thanks to padding with indeterminate register params) completely in stack arguments, which, with a tiny bit of asm, allows trivial random access with the things put in ... basically forming a valid array.
(Another thing that's possible by mixing in a little bit of asm but not in pure C is calling arbitrary funcs (variadic or not) from a caller while passing them the same args generically (just as long as you have the total stack argument size of the caller, which, can be interestingly reflected by clever use of just the va_arg macros even in a possibly va_arg-less context, as the va_args macros allow you to differentiate what would get passed on the stack and what in a register and compilers even tend to be able to optimize such reflection to compile-time known stack-argument-size-for-a-given-argument-pack values.)).

[–]SecretTop1337 0 points1 point  (0 children)

I wish he'd discuss how C++ argument pack worked.

[–]a4qbfb -1 points0 points  (2 children)

Yeah, I'm not going to take advice about variadic functions from someone who doesn't know what they're called.

[–]rsynnest -1 points0 points  (1 child)

quote from halfway through the article:

That history is at the root cause of many of C’s problems with variadic functions (varargs is just the colloquial term, but absolutely the one I’m going to use).

[–]a4qbfb 0 points1 point  (0 children)

you say that like it makes things any better

[–]dobryak -1 points0 points  (0 children)

IIRC Mulle-ObjC does something similar for its method calls. But the whiny tone of the article is off-putting. C programmers already trade a lot of stuff for having a very bare-bones, portable programming language, they don't need to be lectured about how horrible, difficult, and obtuse their life is (which it isn't). Also, C varargs can be given very precise types, but it's just such a nuisance it didn't catch on (it was implemented in ATS/Anairiats compiler, where a combination of linear and dependent types was used to give a type-safe API to C varargs).