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

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

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

Author: Jeff Preshing

Published: 2018-01-16T14:21:00Z

Content type: article

Language: en

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

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Game engine](<https://devfeed.tech/topics/game-engine.md>), [JSON](<https://devfeed.tech/topics/json.md>), [OpenGL](<https://devfeed.tech/topics/opengl.md>), [3D rendering](<https://devfeed.tech/topics/3d-rendering.md>), [GitHub](<https://devfeed.tech/topics/github.md>)

Tags: [3d-rendering](<https://devfeed.tech/tags/3d-rendering.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [code](<https://devfeed.tech/tags/code.md>), [github](<https://devfeed.tech/tags/github.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [json](<https://devfeed.tech/tags/json.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This article presents a small runtime reflection system for C++11. It explains how runtime-created type descriptors represent C++ type metadata and support serialization, rendering, graphics programming, and JSON-based asset importing in a custom game engine.

## Source excerpt

In this post, I'll present a small, flexible system for runtime reflection using C++11 language features. This is a system to generate metadata for C++ types. The metadata takes the form of TypeDescriptor objects, created at runtime, that describe the structure of other runtime objects. I'll call these objects type descriptors. My initial motivation for writing a reflection system was to support serialization in my custom C++ game engine, since I have very specific needs. Once that worked, I began to use runtime reflection for other engine features, too: 3D rendering: Every time the game engine draws something using OpenGL ES, it uses reflection to pass uniform parameters and describe vertex formats to the API. It makes graphics programming much more productive! Importing JSON: The engine's asset pipeline has a generic routine to synthesize a C++ object from a JSON file and a type descriptor. It's used to import 3D models, level definitions and other assets. This reflection system is based on preprocessor macros and templates. C++, at least in its current form, was not designed to make runtime reflection easy. As anyone who's written one knows, it's tough to design a reflection system that's easy to use, easily extended, and that actually works. I was burned many times by obscure language rules, order-of-initialization bugs and corner cases before settling on the system I have today. To illustrate how it works, I've published a sample project on GitHub: This sample doesn't actually use my game engine's reflection system. It uses a tiny reflection system of its own, but the most interesting part - the way type descriptors are created, structured and found - is almost identical. That's the part I'll focus on in this post. In the next post, I'll discuss how the system can be extended. This post is meant for programmers who are interested in how to develop a runtime reflection system, not just use one. It touches on many advanced features of C++, but the sample project