4 ms·
Similar to making all your computations in the expressions for default arguments in python (and/or C++) and leaving the function bodies empty. Fancy, but not th
by AshamedCaptain 4y ago
Similar to making all your computations in the expressions for default arguments in python (and/or C++) and leaving the function bodies empty. Fancy, but not that mindboggling. What may surprise people is how and when these expressions are evaluated since they differ between languages.
- jstanley 4y agoIt might not be mind-boggling, but it is highly unusual to people who "think in C". This is not C! This kind of stuff is why purists stick to C89. I'm uneasy about variable-length arrays at the best of times. It hadn't even occurred to me that you might have a variable-length array as a function argument.
- jsmith45 4y agoVLAs in a prototype are sensible, because they decays to a pointer, and is thus functioning purely as documentation. In the function definition, it seems to basically also decay to a pointer, except that the compiler adds basically an assertion that the value in the brackets is greater than zero to the to the top of the function. That actually seems quite weird to me. I'd have been fine with the VLA acting like a proper local VLA with respect to things like sizeof, in which case the assertion makes sense. Or I'd have also been fine with it totally decaying to a pointer, making it just documentation. This half-way in between state is quite weird.
- unnah 4y agoNote that sizeof is useful on the inner dimensions of multi-dimensional array arguments: the following prints the value of m. int foo(int n, int m, double x[n][m]) { printf("sizeof(x[0])=%zd\n", sizeof(x[0])/sizeof(double)); }
- silon42 4y agoor RIIR is suddenly preferable.
- AlotOfReading 4y agoSerious question, but what "purists" remain on ANSI C? Even Linux has abandoned it as of 5.19. The only times I use it are when I'm writing something that requires ridiculous portability even to bizarre platforms and compilers. C99 is far more ergonomic in comparison, even if you have to avoid using some of the truly terrible design decisions that came along for the ride.
- alar44 4y agoEmbedded probably.
- AlotOfReading 4y agoThat's where I work. I haven't seen c89 in years outside those "extreme portability" projects like Linux and curl.
- arinlen 4y ago> Serious question, but what "purists" remain on ANSI C? It means nothing. It's a baseless attemt at an appeal to authority that has no merit.
- MayeulC 4y agoOh, but designated initializers are so useful!
- arinlen 4y ago> This is not C! This kind of stuff is why purists stick to C89. This personal assertion holds no water. People hold/held onto C89 because compilers like Visual Studio's C compiler failed to support anything beyond C89 for ages. https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-... It's my understanding that Microsoft adopted a role in the C standardization committee that was a kin to sabotaging any update beyond C89.
- pjmlp 4y agoMicrosoft saw no value in supporting C when C++ is a better option [0], they caved in because Microsoft Loves Linux (alongside key FOSS projects) implies loving C as well. Note that they aren't supporting the optional annexes and stuff like C atomics aren't supported. [0] - https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an...
- Koshkin 4y ago> purists stick to C89 But the true Scotsman… uh, purist sticks to the K&R C as C89 has already lost some of the original elegance and simplicity. (Never mind that it’s a challenge since the general community has moved on.)
- josefx 4y ago> What may surprise people is how and when these expressions are evaluated since they differ between languages. What could possibly be surprising about reusing the exact same object on every function call, especially when you default to an empty list, set or dict. Getting an actual empty list is as easy as defaulting to None and explicitly checking for it, so there isn't even a reason to do it any differently. /s Who the hell thought that this was sane behavior in a language with mostly mutable objects?
- omoikane 4y agoIt's not quite the same as default arguments in that default arguments are evaluated at run time, whereas array lengths for C++ (and not C) needs to be a compile time constant expression. #include <stdio.h> // "puts" makes "f" not constexpr. constexpr int f() { return puts("hello"); } // error: size of array 'argv' is not an integral constant-expression int main(int argc, char *argv[f()]) {}
- MayeulC 4y agoAs you point out, VLAs in C are evaluated at run time too, making it very similar to abusing default arguments.
- planede 4y agoC++ doesn't allow accessing other function arguments in a default argument, is in some sense the VLA trick is more powerful.
- Izkata 4y ago> Similar to making all your computations in the expressions for default arguments in python Python default arguments are evaluated only once at function definition, not every function call. It's the source of one interesting WTF for anyone who assumes otherwise: def foo(a=[]): a.append(1) return a >>> foo() [1] >>> foo() [1, 1] >>> foo() [1, 1, 1]