Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Classes and structs

Exclude members and classes

Sometimes, you may want to exclude classes and/or members from the bindings. Look at the example below for instructions:

Loading...

Default constructor with named params

litgen will automatically generate a default constructor with named params for structs. For classes, you can use options.class_create_default_named_ctor__regex. This constructor is generated only if the class/struct does not provide a default constructor.

See example below:

Loading...

Expose protected member functions

Relevant portion of the pybind11 manual and of the nanobind manual

Exposing protected member functions requires the creation of a “Publicist” helper class. litgen enables to automate this:

Loading...

Overriding virtual methods in Python

Relevant portion of the pybind11 manual and of the nanobind manual

In order to override virtual methods in Python, you need to create of a trampoline class, which can be a bit cumbersome.

litgen can automate this: look at the pybind11 binding code, and at the glue code below.

Loading...

Note: in the case of nanobind, the glue code will differ a bit. It is shown below:

Loading...

Combining virtual functions and inheritance

Relevant portion of the pybind11 manual and of the nanobind manual

Loading...

Note: in the case of nanobind, the glue code will differ a bit. It is shown below:

Loading...

Operator overloading

litgen is able to automatically transform C++ numerical operators into their corresponding dunder function in Python.

Overloading addition, substraction, etc.

See example below:

Loading...

Overloading comparisons with the spaceship operator

Loading...

Dynamic attributes

Relevant portion of the pybind11 manual and of the nanobind manual

The python class Pet below will be able to pick up new attributes dynamically:

Loading...

Copy and Deep copy

Relevant portion of the pybind11 manual. The principle is the same for nanobind.

See below for instructions on how to add support for copy and deepcopy.

Loading...

Iterable classes

It is possible to make custom container classes iterable in python. See example below:

Loading...

Numeric C array members

If a struct/class stores a numeric C array member, it will be exposed as a modifiable numpy array.

Loading...

Inner class or enum

litgen handles inner classes or enum and correctly nest them in their host class:

Loading...