# A Flexible Reflection System in C++: Part 2

DevFeed: [A Flexible Reflection System in C++: Part 2](<https://devfeed.tech/articles/a-flexible-reflection-system-in-c-part-2-21014.md>)

Original publisher: [Read original article](<https://preshing.com/20180124/a-flexible-reflection-system-in-cpp-part-2>)

Author: Jeff Preshing

Published: 2018-01-24T13:07:00Z

Content type: tutorial

Language: en

Sources: [Jeff Preshing](<https://devfeed.tech/sources/jeff-preshing.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>)

## AI overview

This tutorial extends a flexible runtime reflection system in C++11 to support additional built-in types. It demonstrates adding a type descriptor for double, explains how template specialization resolves primitive descriptors, and discusses extending the system for custom types such as OpenGL-related classes.

## Source excerpt

In the previous post, I presented a basic system for runtime reflection in C++11. The post included a sample project that created a type descriptor using a block of macros: // Define Node's type descriptor REFLECT_STRUCT_BEGIN(Node) REFLECT_STRUCT_MEMBER(key) REFLECT_STRUCT_MEMBER(value) REFLECT_STRUCT_MEMBER(children) REFLECT_STRUCT_END() At runtime, the type descriptor was found by calling reflect::TypeResolver<Node>::get(). This reflection system is small but very flexible. In this post, I'll extend it to support additional built-in types. You can clone the project from GitHub to follow along. At the end, I'll discuss other ways to extend the system. Adding Support for double In Main.cpp, let's change the definition of Node so that it contains a double instead of an int: struct Node { std::string key; double value; std::vector<Node> children; REFLECT() // Enable reflection for this type }; Now, when we build the sample project, we get a link error: error: unresolved external symbol "reflect::getPrimitiveDescriptor<double>()" That's because the reflection system doesn't support double yet. To add support, add the following code near the bottom of Primitives.cpp, inside the reflect namespace. The highlighted line defines the missing function that the linker complained about. //-------------------------------------------------------- // A type descriptor for double //-------------------------------------------------------- struct TypeDescriptor_Double : TypeDescriptor { TypeDescriptor_Double() : TypeDescriptor{"double", sizeof(double)} { } virtual void dump(const void* obj, int /* unused */) const override { std::cout << "double{" << *(const double*) obj << "}"; } }; template <> TypeDescriptor* getPrimitiveDescriptor<double>() { static TypeDescriptor_Double typeDesc; return &typeDesc; } Now, when we run the program - which creates a Node object and dumps it to the console - we get the following output instead. As expected, members that were previously int are now do