Live data from Hacker News

Reflection in C++

donw.io

31–40 of 40 posts

Re: Reflection in C++

#31

"I made up the term 'object-oriented', and I can tell you I didn't have C++ in mind" Alan Kay (inventor of Smalltalk)

Upvoted for the value of that quote (it's new to me), despite it not contributing to the discussion.

Re: Reflection in C++

#32

Earlier quoted context omitted.

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.

Isn't code bloat "defined" as code that is unnecessary? If it's used, it's not unnecessary. In C# with generics, the JIT compiler will generate seperate code paths for List, List, List, etc. in order to speed up execution. The only difference with C++ templates is that it affects the executable size and is done before hand instead of at runtime (or ngen time).

Re: Reflection in C++

#33
This seems like a bad idea. It looks like the author is attempting to reinvent java in C (there is no c++ here) because he really likes c# or java.

Most applications use an embedded interpreter to do runtime inspection and modification. The tools for the job is usually lua/python. Creating your own type system seems like a bad idea.

Re: Reflection in C++

#34

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. R…

Great explanation, thanks.

Re: Reflection in C++

#35

Earlier quoted context omitted.

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 eleme…

Just as it is possible to implement a std::vector-like container using void* without templates, it is possible to do it with templates and partial specialization. It's a question of how much work the implementor put into things.

Re: Reflection in C++

#37
post #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.

It is because you can do all sorts of nice things in a data driven way. E.g. your reflected code might expose a load of objects and behaviours. At runtime you could load a data file which contains information to create objects, and hook up the behaviour. Loading (and reloading) scripts or data files can be significantly faster than recompiling a large game. It also allows non-programmers to contribute.

Re: Reflection in C++

#38
post #4

"is a bad idea".

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…

These are the reasons I'm interested in C++ reflection. I am maintaining a library called Ponder on Github with this in mind. http://billyquith.github.io/ponder/

I'm not interested in exposing everything in an executable. I see that as unnecessary. I just want the relevant objects exposed in a useful way.

Re: Reflection in C++

#39
post #8

http://xmlrpc-c.sourceforge.net/ is a real world implementation

It shares similar traits, yes. You can interrogate something to find out what API it provides for you. The transport here is XML (SOAP?) and the binding is written in C. However, we are discussing reflection in C++, which is more generic than this. A C++ reflection library would not have one specific purpose like this.

Re: Reflection in C++

#40

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")

Not to dampen your enthusiasm but it may not happen anytime soon. An explanation, and a solution, is offered on my blog: http://chinbilly.blogspot.co.uk/2016/03/ponder-c-reflection....
Post reply on HN