r/cpp • u/_bstaletic • Jun 21 '26
Pymetabind: Writing pybind11 bindings with C++26 reflections
https://codeberg.org/bstaletic/pymetabind/
81
Upvotes
r/cpp • u/_bstaletic • Jun 21 '26
View all comments
Show parent comment
1
u/_bstaletic Jun 21 '26 edited Jun 21 '26
For strings, it was just easier to have a
fixed_stringtemplate argument, than aconst char*data member, because structural types are important.BEGIN EDIT
Also,
const char*can't point at a string literal, if it is to be passed toreflect_constant, because string literals can not be function parameters.END EDIT
I did not talk about
extra_attributes(i.e. return value policies, keep_alive and call guards), but it looks like this:https://codeberg.org/bstaletic/pymetabind/src/branch/master/include/pymetabind/bind/common.hpp#L14-L25
And I use it like this:
The problem with that is:
...does not compile, because
guardsis consteval-only, which meansextra_attributesis consteval-only, which meansmaybe_keep_alive...is consteval-only, which meansmaybe_keep_alive...can not be used in a call to a non-constant-evaluated expression, likepy::class_::def().The solution is:
This works because
keep_alive_copyis not consteval, but "only" constexpr.Defensiveness aside, I do need to take a closer look at what really does need to remain a template.