4 ms·
Poor, poor C++... cries in C
by oilkillsbirds 2y ago
Poor, poor C++... cries in C
- anitil 2y agoI've seen cases where people do things like my_func(/*arg1=*/val1,/*arg2=*/val2) And I suppose you could write a validator to make sure that this worked. Or using an anonymous structure in C99, or a named structure in C89. And of course a pointer if you care about register/stack usage etc. I'm not sure what the other options are.
- ranger_danger 2y agoat that point why not just pass in a struct as your argument
- anitil 2y agoI suppose it depends on your application. I worked in embedded devices where stack was limited and the compiler was .... less than reliable about how it would manage passing a large struct to your function, which is why I mentioned passing in a pointer instead
- ranger_danger 2y agoyes a pointer to struct would be acceptable as well
- RossBencina 2y agoIn 1998 perhaps. It wouldn't be acceptable to today's C++ value-semantics crowd. Are you sure that you want to take the indirection penalty? or that the compiler will optimise it away?
- BodkinsOdds 2y agoC++ is already so reliant on optimizing `const&` parameters that I can't imagine why you wouldn't use them if you happen to be stuck with a shitty embedded toolchain (or MSVC) that can't be relied on to pass struct parameters efficiently.
- gpderetta 2y agoIf stack space usage is an issue, then it means that the struct is already being passed by hidden pointer (as opposed to using a register passing calling convention). Making the pointer explicit won't have any effect on performance (but it will on semantics).
- jcelerier 2y agodepending on the platform this may cause things to be pushed / poped on the stack instead of being passed as registers
- tjalfi 2y agoC++20 added designated initializers, so they're also an option. my_func({.arg1 = val1, .arg2 = val2});
- anitil 2y agoOh I thought that was a C99 addition? It's been a while since I've used them. Edit: struggling to find a source, though this GCC doc suggests it's C99 (but maybe that's only GCC?) - https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
- AlotOfReading 2y agoIt was C99. It took C++ 21 years to copy it.
- eqvinox 2y agoIt's been the primary and most annoying interop issue for us when we have to integrate C++ code into our primarily C codebase (= build some pieces of our code in C++, which necessarily has to interact with at least our header files). (Second place: differences in available GCC/clang compiler extensions between C and C++ [our software does not support Windows/MSVC], third place: differences in what casts are permitted/how they are done)
- uecker 2y agoMy number one issue are VLA parameters int foo(int N, char buf[N]); which I like to use because they improve warning messages, but they are not accepted in C++.
- Cyph0n 2y agoWell, C is not the same as C++. It even says in that doc that “this extension is not implemented in C++”.
- 2y ago
- brandmeyer 2y ago> And I suppose you could write a validator to make sure that this worked. Like this one! https://clang.llvm.org/extra/clang-tidy/checks/bugprone/argument-comment.html https://clang.llvm.org/extra/clang-tidy/checks/bugprone/argu...
- anitil 2y agoWow I had no idea, that's great!
- wffurr 2y ago>> I suppose you could write a validator to make sure that this worked No need to write it: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/argument-comment.html https://clang.llvm.org/extra/clang-tidy/checks/bugprone/argu... Efb
- anitil 2y agoThat's great, I wish I'd known about this ages ago