4 ms·
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 sy
by SideQuark 2y ago
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_languages_and_platforms https://en.wikipedia.org/wiki/List_of_reflective_programming...
- sky2224 2y agoIsn'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.
- arjonagelhout 2y agoWhen 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.
- harry8 2y ago>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).
- vouwfietsman 2y agoTakes 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.
- deleted 2y ago[deleted]
- Tsche 2y agoThis 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.
- vouwfietsman 2y agoInteresting! 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.
- jcelerier 2y agoC++ 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.
- logicchains 2y ago>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.
- weinzierl 2y agoThe 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.
- flohofwoe 2y agoI'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 ;)
- deleted 2y ago[deleted]
- jcranmer 2y agoThe 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".
- xmcqdpt2 2y agoHaving 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.
- pjmlp 2y agoWe 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.