Re:Fun with C++26 reflection: Keyword Arguments
by oilkillsbirds on Monday February 10, 2025 @07:41PM
(#43007395)
Poor, poor C++... cries in C
[ Parent
| on HN ]
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Monday February 10, 2025 @08:50PM
(#43008051)
I'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.
Re:Fun with C++26 reflection: Keyword Arguments
by wffurr on Monday February 10, 2025 @09:53PM
(#43008416)
>> 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/argu...
Efb
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Tuesday February 11, 2025 @03:44AM
(#43010464)
That's great, I wish I'd known about this ages ago
Re:Fun with C++26 reflection: Keyword Arguments
by tjalfi on Monday February 10, 2025 @09:43PM
(#43008346)
C++20 added designated initializers, so they're also an option.
my_func({.arg1 = val1, .arg2 = val2});
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Monday February 10, 2025 @09:58PM
(#43008435)
Oh 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
Re:Fun with C++26 reflection: Keyword Arguments
by AlotOfReading on Monday February 10, 2025 @10:07PM
(#43008486)
It was C99. It took C++ 21 years to copy it.
Re:Fun with C++26 reflection: Keyword Arguments
by eqvinox on Tuesday February 11, 2025 @02:04AM
(#43009841)
It'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)
Re:Fun with C++26 reflection: Keyword Arguments
by uecker on Tuesday February 11, 2025 @12:04PM
(#43015219)
My 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++.
Re:Fun with C++26 reflection: Keyword Arguments
by Cyph0n on Monday February 10, 2025 @10:07PM
(#43008487)
Well, C is not the same as C++. It even says in that doc that “this extension is not implemented in C++”.
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Monday February 10, 2025 @10:43PM
(#43008683)
Yeah sorry in my head I assumed that it would have been adopted
Re:Fun with C++26 reflection: Keyword Arguments
by pragma_x on Tuesday February 11, 2025 @09:40AM
(#43013200)
Even though you need to declare a struct for this (or other) functions to receive these fields, I feel like this is the cleanest approach. What I'm not sure about is if reference, value, or pointer is the best way to let the compiler optimize this.
Re:Fun with C++26 reflection: Keyword Arguments
by brandmeyer on Monday February 10, 2025 @09:47PM
(#43008372)
> 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/argu...
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Tuesday February 11, 2025 @03:43AM
(#43010459)
Wow I had no idea, that's great!
Re:Fun with C++26 reflection: Keyword Arguments
by ranger_danger on Monday February 10, 2025 @09:41PM
(#43008338)
at that point why not just pass in a struct as your argument
Re:Fun with C++26 reflection: Keyword Arguments
by jcelerier on Tuesday February 11, 2025 @12:05AM
(#43009182)
depending on the platform this may cause things to be pushed / poped on the stack instead of being passed as registers
Re:Fun with C++26 reflection: Keyword Arguments
by anitil on Monday February 10, 2025 @10:02PM
(#43008454)
I 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
Re:Fun with C++26 reflection: Keyword Arguments
by ranger_danger on Monday February 10, 2025 @11:26PM
(#43008961)
yes a pointer to struct would be acceptable as well
Re:Fun with C++26 reflection: Keyword Arguments
by RossBencina on Tuesday February 11, 2025 @12:28AM
(#43009302)
In 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?
Re:Fun with C++26 reflection: Keyword Arguments
by BodkinsOdds on Tuesday February 11, 2025 @01:47AM
(#43009745)
C++ 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.
Re:Fun with C++26 reflection: Keyword Arguments
by gpderetta on Tuesday February 11, 2025 @06:33AM
(#43011629)
If 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).