Using Reflection to Generate C++ Python Bindings

Callum Piper

⏱ 20 mins
intermediate
advanced
15:10-15:30, Thursday, 3rd April 2025
Python bindings for C++ can be valuable tools for developers who wish to make C++ libraries available for use in Python. There are many reasons developers may wish to do this, including testing, prototyping, or performance.

One of the issues often faced when writing Python bindings for C++ is the need to write excessive amounts of boilerplate code to bridge the gap between the C++ types and interfaces and their Python equivalents. Some libraries make this easier than others, but there is always overhead.

One of the easier and more commonly used libraries is PyBind11. This talk will explore how you can use Reflection in C++ to generate some of the necessary boilerplate code required when using PyBind11. We will present real-world examples that can be compiled using the latest experimental versions of compilers that support Reflection, and we'll also generate genuine, usable Python bindings. While these compilers are still under development, this will still highlight how the use of Reflection can simplify -- and abstract away -- the generation of some simple boilerplate code. We will also look at the limitations of such an approach and what we might aim for in the future.

🏷 reflection
🏷 python
🏷 bindings
🏷 pybind11
🏷 C++26

Callum Piper

Callum Piper has been writing C++ since 2000. He has spent five years as a Senior Software Engineer at Bloomberg, working on Derivatives Pricing services. Prior to joining Bloomberg, Callum was a consultant for more than 10 years, during which he worked on a wide range of different teams across a number of financial, tech, and retail companies -- with vastly different experiences and results. He started his career at a small robotics company outside Cambridge that did work in the pharmaceutical industry. Callum is very interested in both understanding and improving how teams solve problems and deliver high quality software solutions for clients.