News for hackers. Stuff that matters.
Re:Fun with C++26 reflection: Keyword Arguments by SideQuark on Monday February 10, 2025 @08:40PM (#43007958)
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...

Re:Fun with C++26 reflection: Keyword Arguments by sky2224 on Monday February 10, 2025 @11:17PM (#43008904)
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 date. When used correctly, reflection can be quite elegant, but I think those cases are seldom advantageous in comparison to a different likely better design approach.

Re:Fun with C++26 reflection: Keyword Arguments by jcelerier on Tuesday February 11, 2025 @12:04AM (#43009173)
C++ compilers also had this information since forever(basically reflection is giving to the end user some level of access to the AST nodes information) - the objections against reflection were always more political or about end-user design of the feature.
Re:Fun with C++26 reflection: Keyword Arguments by arjonagelhout on Monday February 10, 2025 @11:50PM (#43009098)
When I started out programming in C#, I used reflection sometimes to circumvent the language’s design and restrictions. This resulted in brittle and hard to reason about code. Reflection should never be used to do this.

So I do think you’re right that reflection can (and will) be abused by beginners if present as a language feature.

With that said, I don’t know much about the internals of the C++ compiler, but having built a simple reflection system for C++, I think the important thing is just being able to serialize and deserialize POD structs to and from some representation (e.g. json). For more advanced data, such as images or specific binary (file) formats, it’s easier to write custom writing / reading, encoding / decoding logic.

Re:Fun with C++26 reflection: Keyword Arguments by harry8 on Tuesday February 11, 2025 @02:38AM (#43010061)
>I think the important thing is just being able to serialize and deserialize POD structs to and from some representation (e.g. json). For more advanced data, such as images or specific binary (file) formats, it’s easier to write custom writing / reading, encoding / decoding logic.

Why not just use a visitor? (Genuine question, not c++ snark).

Re:Fun with C++26 reflection: Keyword Arguments by vouwfietsman on Tuesday February 11, 2025 @05:16AM (#43011036)
Takes a bunch of manual work per POD, that's the gist of it.

Fundamentally, if I have struct { int x = 3; std::string y = "bla"; }

I want the json to look like { x: 3, y: "bla" } without writing any code specific to the struct, and have it work bidirectionally. This is currently not possible because of a lot of reasons, but most trivially because the names "x" and "y" do not even exist anymore in your compiled code.

Re:Fun with C++26 reflection: Keyword Arguments by Tsche on Tuesday February 11, 2025 @04:19PM (#43018513)
This is absolutely possible, even currently - albeit in a very much non-portable way. For example boost::pfr and my own (wip) repr library have the required machinery for this.
Re:Fun with C++26 reflection: Keyword Arguments by vouwfietsman on Thursday February 13, 2025 @05:11AM (#43034498)
Interesting! There seem to be a lot of limitations though:

> Boost.PFR library works with types that satisfy the requirements of SimpleAggregate: aggregate types without base classes, const fields, references, or C arrays:

And in general seems to be dependent on C++20 for getting field names.

Do you know how this works? Initializer lists seem somehow involved.

Re:Fun with C++26 reflection: Keyword Arguments by logicchains on Tuesday February 11, 2025 @12:49AM (#43009405)
>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?

C++26 reflection is compile-time reflection, not runtime reflectiom, it doesn't require any extra information in the binary. It just uses information that the compiler already has at compilation time.

Re:Fun with C++26 reflection: Keyword Arguments by weinzierl on Tuesday February 11, 2025 @02:37AM (#43010053)
The article is about compile-time reflection, which is not nearly as useful as what the Wikipedia article you linked is about. It's only upside is that it is "zero cost" by some definitions of cost.

Runtime reflection on the other hand is useful but it also puts up obstacles not easy to overcome. For example it is one major reason we still have no decent ahead-of-time compilation in Java.

Re:Fun with C++26 reflection: Keyword Arguments by flohofwoe on Tuesday February 11, 2025 @02:59AM (#43010185)
I'm in the opposite camp, runtime reflection is useless, and almost always points to a design flaw while compile time reflection is actually useful (for obvious things like automatically building a serialization layer or an UI which represents the type, or building types from other types).

I'm sure that C++26 has implemented it in a way which is highly unpleasant to use though ;)

Re:Fun with C++26 reflection: Keyword Arguments by jcranmer on Tuesday February 11, 2025 @09:59AM (#43013486)
The element of runtime reflection that is the most useful is something along the lines of "here's function, here's a list of arguments, go call this function with those arguments".
Re:Fun with C++26 reflection: Keyword Arguments by xmcqdpt2 on Wednesday February 12, 2025 @07:04AM (#43024492)
Having to do this by reflection is a problem in principle solvable by types. Here is a function of type (A, B, C) => R, and here is a tuple of arguments of type (A, B, C), go call this function with those arguments.

Most programming languages make it hard to easily express those constraints so reflection is used instead.

Re:Fun with C++26 reflection: Keyword Arguments by nickelpro on Tuesday February 11, 2025 @03:40AM (#43010434)
Compile-time reflection is by far the most useful

It replaces the plethora of code generators that currently surround C++ like flies

Re:Fun with C++26 reflection: Keyword Arguments by pjmlp on Tuesday February 11, 2025 @03:00AM (#43010188)
We have add decent AOT compilation in Java almost since 2000, with Excelsior JET being one of the first vendors to offer it, but few are willing to pay for such tools.

Also I consider ART, GraalVM and OpenJ9 decent enough as free beer AOT.

Re:Fun with C++26 reflection: Keyword Arguments by badmintonbaseba on Tuesday February 11, 2025 @06:38AM (#43011666)
You can build runtime reflection on top of compile time reflection, it's impossible the other way around. Compile time reflection is a superset of what runtime reflection can do.