Motivation for looking at Reflection in C++

At work, we have two frameworks for developing new components – one in C++ and the other in F#. The F# framework is newer and benefits from the insight of previous years working with the C++ framework. In particular, the new F# framework only requires developers to implement a reduced, strongly typed interface in F# by defining a few types (e.g. records) and associated functions between them. This reduced interface is inflated into a full model by the framework, making heavy use of .NET Reflection.

The F# framework has delivered productivity improvements (development time down to a third compared to the previous C++ framework). But the philosophical question remains – how much of that is due to the new architecture developed with the benefit of hindsight? And could we replicate that architecture in C++? The main functionality gap comes down to this: in F# you can use .NET Reflection to discover the names and types of fields in F# types such as unions, records and options, but in C++ you can’t.

Requirements

A full implementation of Reflection for C++ would include ability to discover type information, field names, properties and methods, as well as being able to create instances of types and invoke methods. I’m interested in a small subset of that scope – the ability to discover the names and values of fields in a C++ struct.

Output

Conclusions

The code above ‘works’ – it satisfies the requirements by providing a way to decorate a struct with metadata that can be used to return the names and values of each field. However, in Visual Studio 2010 and 2012, it produces compiler warnings (due to the macro hackery in TYPE_ONLY) and confuses intellisense (which doesn’t cope with the REFLECTABLE macro). In practical terms, that makes it unsuitable, because the productivity benefits are lost when intellisense and auto-complete stop working.

Thanks Paul – I agree that’s an improvement over the TYPE_ONLY macro in the posted code. Unfortunately, it still doesn’t play nicely with intellisense in Visual Studio though. I’m not giving up yet though!