Hacker News new | ask | show | jobs
by sapek 4008 days ago
There is a really interesting proposal to add reflection to the language: N4447 [1] (N3951 before). The idea is based on an observation that C++ already sort of has reflection-like syntax in form of parameter pack expansion [2]. I find this approach to be a very elegant and in the spirit of modern C++. It is also very powerful. Bond serialization is implemented using reflection. Because C++ doesn't have reflection, for normal classes we have to use code generation to provide type metadata. But for std::tuple<T...> all the necessary information is already available and Bond can serialize arbitrary instances of std::tuple<T...> today [3].

[1] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n444...

[2] http://en.cppreference.com/w/cpp/language/parameter_pack

[3] https://microsoft.github.io/bond/manual/bond_cpp.html#tuples

3 comments

As another example, Firefox uses "manual reflection" to e.g. build lists of all the smart pointers in some classes. It then uses these lists as part of its "cycle collector", which breaks cycles in the refcounted graph of objects.

Writing and maintaining these lists is error-prone, and at least used to be a major source of memory leaks. You can imagine writing a static analysis to figure this out, and the Moz folks may have since I last checked, but if the language could do it, that would be so much better...

As a somewhat-related data point, Servo is already using the macro deriving infrastructure of Rust to generate these lists at compile time for its garbage collector.
I understand the urge to evangelise, but does Rust have to crop up in every C++ thread?
It was in response to a comment that was referring to Firefox. Servo is touted as the eventual replacement of Firefox's rendering engine. It's literally a response to a comment about Mozilla's current practices in this area explaining their proposed future practices.

Seems appropriate to me.

Apologies if I made it seem like evangelism; just wanted to clarify the state of tracing cycle collection in Firefox-related projects.
Reading through N4447, correct me if I'm mistaken, but it seems to be more about compile-time metaprogramming than it does runtime reflection? It does talk about class.for_name(std::string) but kind of handwaves about how it would work.
You are right, it is compile-time reflection, which I think is the most interesting one for C++.
Thanks for bringing Bond up: Never heard of it (even though it passed by on HN at least two times apparentely) but it sure is interesting.