Live data from Hacker News

Reflection in C++

donw.io

21–30 of 40 posts

Re: Reflection in C++

#21
post #13

Earlier quoted context omitted.

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?

Definitely. That's why I'm to hyped for compile-time reflection in C++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/ (search for "reflection")

Re: Reflection in C++

#22

Earlier quoted context omitted.

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

Definitely. That's why I'm to hyped for compile-time reflection in C++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/ (search for "reflection")

One of the authors of that paper, Matúš Chochlík, has been working on this for years. He has a pretty nice implementation that I've used successfully on a number of occasions, even though it requires a pre-compiler pass to generate the necessary data.

If I'm not mistaken, he has been working into getting the necessary data available from the compiler directly (which this paper would confirm).

Link to his existing library: http://kifri.fri.uniza.sk/~chochlik/mirror-lib/html/

Absolutely great guy, as well. Really happy to see him still working on this, year over year.

Edit: DYAC.

Re: Reflection in C++

#23

Earlier quoted context omitted.

In game development it tends to help solve some common problems. Games need a lot of iteration, C++ compile times can easily get very long i.e. minutes. You might object that compile times should never be long: "oh this never happens if you do X or Y" but in reality every C++ gamedev shop I've worked at has had long compiles times. Templates and other things of that nature tend to bloat it. Anyway so reflection helps…

The first two reasons are development/debugging concerns, and the last two are somewhat hacky. Could you elaborate how reflection is the optimal solution for your last two examples?

I would say the optimal solution for the last 2 depends on the total requirements - aka there is no general optimal solution.

Both concerns are about having the need for boilerplate code (either for bindings or for serialization), which is quite repetitive to write and depends on the input types.

Now the possible solutions are from my perspective:

- Write that code by hand, which is quite tedious and error-prone.

- Use some code generator tooling (like protoc) for it. However that might require you to define your types in an IDL before going to code, which leads to an extra step in the development and build process. In my experience this works really well for big teams (because you also have an extra interface documentation against which all parties can work), but it might not be optimal for small teams and rapid iterations.

- Generate the code automatically from the type definitions in your program. Either during compile-time (which would require good compiler support for extracting the relevant details and using them for code generation) or during runtime by reflection. With reflection you are most likely getting the slowest code. But iteration times should be the fastest (no need to change IDL files, run codegen tools, etc.)

Re: Reflection in C++

#24

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…

[deleted]

Re: Reflection in C++

#25

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…

>>but for some reason people like to count it as code bloat

Again, used code being generated is counted as bloat. This, I can't understand.

Re: Reflection in C++

#27

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…

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.

Not if you implement it as a resizeable array of bytes, which is what a lot of the std::vector-ish structures I see being used in C are. There's just one set of functions which work with different element sizes, not a set of nearly-identical ones for each element size (and sometimes even multiple identical ones for elements of the same size but different type.) It's definitely bloat when the result is highly redundant and bigger than necessary.

Re: Reflection in C++

#29
For a statically-compiled languages, I don't see why people would prefer runtime reflection to compile-time code generation. You can reduce code duplication, but at the expense of runtime speed. But if that was the use case, I'm not certain why you would be using C++ to begin with.
Post reply on HN