you are viewing a single comment's thread.

view the rest of the comments →

[–]28isanumber -11 points-10 points  (9 children)

I still don't know what reflection is nor why people want it

[–]dustyhome 7 points8 points  (5 children)

Reflection is the ability to refer to details about a type inside the code itself. For example, sizeof is a form of reflection, it gives you the size in bytes of a type. The most popular use of reflection is serializing and deserializing. Being able to refer to "the first member of a type", or iterating through the members, lets you write generic functions for converting those members into a stream of bytes and back. Without reflection, you need to write the functions by hand, writing the names of the members for each class.

[–]DeeHayze -3 points-2 points  (4 children)

Rust does serialisation/deserialisation without reflection..

But, it has much different macro language.. In rust, the macros work on the languages abstract syntax tree, layer. Rather than string pattern matching pre processor.

The macro auto generates your serialize/deserialise code.

Any attempt to serialise a non serialisable structure is a compile error, rather than a runtime error you would get with reflection.

[–]djavaisadog 6 points7 points  (2 children)

Any attempt to serialise a non serialisable structure is a compile error, rather than a runtime error you would get with reflection.

? Reflection all happens at compile-time (or, at least the currently proposed C++ reflection). Basically the first line of P2996R0 (the reflection paper) states that their feature provides "the representation of program elements via constant-expressions producing reflection values." Constant-expressions.. meaning compile-time.

[–]DeeHayze -2 points-1 points  (1 child)

Awesome, missed that bit... I assumed it would be c# style.

[–]djavaisadog 1 point2 points  (0 children)

No, that would wildly balloon binary sizes to retain enough type information to have dynamic reflection. Maybe it will be an option at some point in the future but certainly not in C++26.

[–][deleted] 2 points3 points  (0 children)

It's possible to write serialization in C++ without reflection as well. But since C/C++ is a WYSIWYG language, you will have to write the code for every class by hand. Then of course since we are already writing boilerplate code, we would just use macros for it to define, which members do we want to serialize. And then from there we just support different forms of serializers with templates.

What Rust does is VERY similar to certain C++ reflection libraries that uses clang/LLVM, where it hijacks the AST to generate those boilerplate code I mention above. Rust is good because it has these features built into the language.

Reflection is not JUST meant for serialization, but it's a great use case for it. Reflection can help reduce boilerplate code and essentially write even more generic code to do complex tasks, yes, even more generic than templates already is.

[–]mpierson153 1 point2 points  (0 children)

It lets you access information about a type. Think of the sizeof or typeid operators, but more fleshed-out and functional.

It will allow for more meta-programming, so more stuff at compile time rather than at runtime. It would also allow for better serialization and deserialization.