A Flexible Reflection System in C++: Part 2

Jeff Preshing · 2018年1月24日

AI 摘要 AI 生成,可能有误

本文扩展了一个基于 C++11 的运行时反射系统,展示了如何添加对新内建类型的支持:像 double 这样的基本类型可通过在任何 .cpp 文件中特化 getPrimitiveDescriptor<>() 函数模板来获得支持,而像 std::unique_ptr<> 这样的模板类型则通过 TypeResolver<> 类模板的偏特化来处理,该偏特化会为每个特化生成相应的类型描述符。文章解释了这些描述符的内部工作原理,即利用模板构造函数和由 lambda 生成的辅助函数来通用地处理每一次实例化。最后,作者讨论了进一步的改进,例如用函数指针表(TypeKey)取代虚函数,以及使用 Python 脚本自动生成反射宏,并指出该系统在一个自定义游戏引擎中得到了大量使用。

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:

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.

继续阅读全文

为了尊重作者,这里只展示开头几段。你可以去原文阅读,或者在 HorizonReader 里读:划线做笔记、让 AI 帮忙。

更多来自 Preshing on Programming

How C++ Resolves a Function Call

AI 摘要The article explains the hidden compile-time algorithm that every C++ compiler uses to resolve a function call, which the author was motivated to understand after repeatedly encountering confusing errors about 'overloads with similar conversions.' It walks through the three main phases of the process—name lookup (including argument-dependent lookup), template argument deduction and substitution (introducing SFINAE), and overload resolution—using a concrete example to show how candidates are found and narrowed down. The author argues that understanding how these steps fit together is especially valuable for programmers developing libraries who combine multiple C++ features.

Flap Hero Code Review

AI 摘要This post reviews the source code of Flap Hero, a small C++ game built without an existing game engine, explaining how it is organized using the Plywood framework and how it differs from larger game projects. The author evaluates the deliberate omission of typical engine subsystems—abstract scene representation, a shader manager, a physics engine, and an asset pipeline—rating each choice as OK, Good, or Bad based on the trade-offs involved. Overall, the article suggests Flap Hero is a valuable learning resource for novice and intermediate game developers, though some simplifications (like hand-managed shaders and no asset pipeline) proved disadvantageous.

A Small Open Source Game In C++

AI 摘要The author has released Flap Hero, a free, open source Flappy Bird clone written entirely in C++, with MIT-licensed source code and public domain assets available on GitHub. The game was created to promote Plywood, a module-oriented open source C++ framework (not a game engine) that integrates third-party libraries like Assimp, SoLoud, stb, and GLFW through its workspace and built-in modules. Flap Hero exemplifies the lean, 'handmade' software style Plywood is designed to support, and the author plans future work such as a GUI build manager, with a source code review to follow in the next blog post.

Automatically Detecting Text Encodings in C++

AI 摘要The article describes how the open-source Plywood C++ framework automatically detects text file formats, addressing the inherent ambiguity of encodings like UTF-8, UTF-16 (with or without BOM), and Windows-1252, along with varying line endings. Plywood examines the first 4KB of a file, checks for valid UTF-8 decoding, and scores alternative encodings using heuristics based on whitespace, ASCII characters, and decoding errors. The author benchmarks this approach against a 50-file test suite, noting that editors like Notepad++, Sublime Text, VS Code, and Windows Notepad achieve comparable detection rates, and invites contributions to improve support for formats like Shift JIS on GitHub.

I/O in Plywood

AI 摘要The article introduces the I/O system of Plywood, an open-source C++ framework, positioning it as a modern alternative to the decades-old standard C and C++ runtime I/O libraries. It explains core concepts such as the buffered OutStream and StringWriter classes, adapter-based chaining for newline conversion and character encoding (e.g., writing a UTF-16 file from UTF-8 input), and buffered text reading with InStream and StringReader, illustrated through code examples. The author also highlights design decisions like OutStream objects not being thread-safe, requiring explicit creation via StdOut::binary() or StdOut::text(), and Plywood's general preference for working with UTF-8 text internally.