Live data from Hacker News

Fun with C++26 reflection: Keyword Arguments

pydong.org

11–20 of 147 posts

Re: Fun with C++26 reflection: Keyword Arguments

#11
post #9

Poor, poor C++... cries in C

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.

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

#12
post #9

Poor, poor C++... cries in C

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.

> 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

#13
post #9

Poor, poor C++... cries in C

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.

>> 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

#14
post #11
post #9

Earlier quoted context omitted.

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.

C++20 added designated initializers, so they're also an option. my_func({.arg1 = val1, .arg2 = val2});

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

#15
post #9

Earlier quoted context omitted.

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.

at that point why not just pass in a struct as your argument

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

#16
post #14
post #11

Earlier quoted context omitted.

C++20 added designated initializers, so they're also an option. my_func({.arg1 = val1, .arg2 = val2});

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

It was C99. It took C++ 21 years to copy it.

Re: Fun with C++26 reflection: Keyword Arguments

#17
post #14
post #11

Earlier quoted context omitted.

C++20 added designated initializers, so they're also an option. my_func({.arg1 = val1, .arg2 = val2});

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

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

#18

I just don't understand why some people are so fascinated by this. Can you all admit that this is not at all practical? I swear C++ folks like it for the sake of it. No other engineer do this. Only antiques people or whatever. Can you imagine an engineer that is adamant on using his mystifying bespoke tool instead of just using a ruler. "But what if I have to measure it in the 4th dimension!?". I was expecting someth…

Personally, I write the simplest subset of C++ I possibly can at all times. Loops, variables, classes and the STL library when useful.

The more advanced features you use, the easier it is to make subtle mistakes, confuse someone who hasn't seen that feature before, or just make everyone working on the software wary of ever touching it.

Re: Fun with C++26 reflection: Keyword Arguments

#19
post #17
post #14

Earlier quoted context omitted.

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

Well, C is not the same as C++. It even says in that doc that “this extension is not implemented in C++”.

Yeah sorry in my head I assumed that it would have been adopted

Re: Fun with C++26 reflection: Keyword Arguments

#20

I just don't understand why some people are so fascinated by this. Can you all admit that this is not at all practical? I swear C++ folks like it for the sake of it. No other engineer do this. Only antiques people or whatever. Can you imagine an engineer that is adamant on using his mystifying bespoke tool instead of just using a ruler. "But what if I have to measure it in the 4th dimension!?". I was expecting someth…

Reflection is such an insanely useful tool that pretty much all modern languages have it. As usual C++ is 20 years behind language design and brings terrible syntax. But it is tremendously useful. https://en.wikipedia.org/wiki/List_of_reflective_programming...

Isn't the reason why so many other languages have reflection due to the fact that they essentially have the information there from the get-go?

For example, C# and Java have their intermediate language that has a bunch of meta information attached to it already or the info can be added easily if need be. Is C++ not more raw in its compilation process from code to object files? I mean sure, I guess meta-data could be added to the object files themselves for runtime inspection, but if it were really that simple, why wouldn't they have just added it if reflection is indeed that much of a sore spot for the language's purpose?

Now, I'd like to add, my understanding of compiler internals is quite limited, so if I'm way off base here, please correct me.

Additionally, while I too find reflection useful, I've seen a lot of cases where it ends up being a bandaid for poor design and gives people an all too enticing shovel to dig themselves deeper into a hole of technical debt. When used correctly, reflection can be quite elegant, but I think those cases are seldom advantageous in comparison to a different and likely better design approach.

Post reply on HN