Live data from Hacker News

Reflection in C++

donw.io

11–20 of 40 posts

Re: Reflection in C++

#11
Can someone please clarify:

"What is Reflection?

A reflection API is a very basic, powerful tool that every game studio should have at their disposal. It normally contains some or all of the following features: ..."

This doesn't actually tell me what reflection is. Telling me what it normally contains or that its something that every game studio should have does not explain the concept. Could someone someone please explain what exactly reflection is as it relates to a language feature?

Re: Reflection in C++

#12
We already do have it, except nobody bothered to package it: DWARF4 debugging information. It's nicely structured, well-documented, easily accessed throught libdwarf; the only remaining thing to do is to package it nicely and submit to boost. It contains complete information about data layout, etc. (otherwise the debugger wouldn't know how to display your data and locate variables).

The downside is that shipping DWARF debugging info alongside of your program is not far from delivering the source code too. I guess it can be post-processed so that it only contains relevant data structures and nothing more.

Similarly, there's a COM component to access PDBs on Windows.

Re: Reflection in C++

#13
post #5
post #4

"is a bad idea".

To clarify, I tried to do something like this back in the 90's and it was _not_ worth the effort. Using something like Google's Protocol Buffers or a similar system where you generate a standard serialization backend for all the plain old data types you want to send across the network is way less of a pain in the ass. If you're concerned about the overhead, look at MsgPack or Cap'n Proto - plenty of ways to shave the…

You're talking about serialization, and I agree that using reflection for that is both an overkill and somewhat hacky (you still have to define the serialization format).

Reflection on the other hand is immensely useful if you want to, for example, automatically generate type-safe bindings for, say, Python or .NET or Java, from your C++ classes.

Re: Reflection in C++

#14
post #12

We already do have it, except nobody bothered to package it: DWARF4 debugging information. It's nicely structured, well-documented, easily accessed throught libdwarf; the only remaining thing to do is to package it nicely and submit to boost. It contains complete information about data layout, etc. (otherwise the debugger wouldn't know how to display your data and locate variables). The downside is that shipping DWAR…

This guy has packaged it: https://github.com/stephenrkell/liballocs/

Re: Reflection in C++

#15
To my knowledge, only code paths that are used are generated through templates. For example if you don't use std::list, you won't get std::list generated. So why does the author say:

>>Template meta-programming approaches suffer from increased compile-times and along with code generation, result in larger than necessary executables.

?

Re: Reflection in C++

#16

To my knowledge, only code paths that are used are generated through templates. For example if you don't use std::list , you won't get std::list generated. So why does the author say: >>Template meta-programming approaches suffer from increased compile-times and along with code generation, result in larger than necessary executables. ?

There are two reasons:

* If you use std::vector, std::vector and std::vector you will (probably) end up with three instantiations of std::vector ("probably" because the implementation has some leeway). This it no different than what you would get if you implemented std::vector with macros or by hand, but for some reason people like to count it as code bloat.

* Modern compilers have a tendency to instantiate the same template multiple times, (e.g., std::vector, std::string) and then get rid of any duplicates ( https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.ht... ). This affects compile times, but if the compiler/linker does a good job of getting rid of the duplicates it shouldn't affect executable size. Sadly, many compilers do a bad job of getting rid of duplicates. extern templates are supposed to help out now ( http://www.stroustrup.com/C++11FAQ.html#extern-templates ).

Re: Reflection in C++

#17

Can someone please clarify: "What is Reflection? A reflection API is a very basic, powerful tool that every game studio should have at their disposal. It normally contains some or all of the following features: ..." This doesn't actually tell me what reflection is. Telling me what it normally contains or that its something that every game studio should have does not explain the concept. Could someone someone please e…

Reflection is the concept of having a program have knowledge about it's own structure. Kinda like having your code look into a mirror.

A really basic example of this is having you code get a list of all the names of fields of a class. This would be very useful if you want to create a generic object serializer.

In the past I used this to create a framework that would generate a UI for you based on class definitions.

Reflection can also include being able to modify your own code structure during run time.

Re: Reflection in C++

#18
post #13
post #5

Earlier quoted context omitted.

To clarify, I tried to do something like this back in the 90's and it was _not_ worth the effort. Using something like Google's Protocol Buffers or a similar system where you generate a standard serialization backend for all the plain old data types you want to send across the network is way less of a pain in the ass. If you're concerned about the overhead, look at MsgPack or Cap'n Proto - plenty of ways to shave the…

You're talking about serialization, and I agree that using reflection for that is both an overkill and somewhat hacky (you still have to define the serialization format). Reflection on the other hand is immensely useful if you want to, for example, automatically generate type-safe bindings for, say, Python or .NET or Java, from your C++ classes.

Wouldn't you prefer to generate type-safe bindings at compile-time?

Re: Reflection in C++

#20
If you search for "reflection" in the list of papers at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/ there are some proposals to add compile-time (as opposed to the run-time reflection described in the article) reflection/introspection to C++.

Of course compile-time reflection can be used to generate the code/data structures necessary for run-time reflection.

P.S: The blog post is from 2011

Post reply on HN