Automatically create bindings using C++26 reflection (example included) #1314
matthewkolbe
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
|
I did a nanobind26 version of this. The test harness repo mentioned above can now use that version or a fork, depending on the shell script arguments. I wouldn't be terribly excited to manage a nanobind26 project. Not because I wouldn't want to put in the work, but because I don't think I'd be terribly good at it. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Having dabbled in other languages that have reflection, the one thing I've gravely missed in C++ is the fact that you have to write and maintain so much boilerplate for external bindings and serialization. But now with P2996 landing in bleeding edge C++ compilers, we can start doing exactly that.
I put together a proof of concept that adds a single function to nanobind:
This automatically binds constructors, data members, methods, static members, enums, and free functions — including overloads. Pass a namespace and it discovers everything inside it recursively. Private/protected members are skipped. No strings, no manual
.def()chains.The implementation is a single header:
include/nanobind/nb_reflect.hon thereflectbranch of my fork, with tests and docs following the existing nanobind conventions.Since P2996 requires a bleeding-edge compiler (GCC trunk or Bloomberg's clang-p2996 fork), I also set up a test harness repo with Dockerfiles that build each compiler from source so anyone can try it without installing anything globally.
This is just a jumping-off point for ideas. There's plenty left to explore (operator mapping, inheritance, virtual functions). But I wanted to show that we can make useful things right now, and that the future of leveraging reflection in nanobind looks very bright.
@wjakob has made it clear that nanobind will stick to C++17, but offered the possibility of a nanobind26 or something of that nature in the future. I personally would be pretty compelled to move to 26 for a feature like this.
Beta Was this translation helpful? Give feedback.
All reactions