Skip to content

How to register a template based function with pybind11 class or module level function? #3662

Answered by jiwaszki
Mayuresh16 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Mayuresh16 ,

It is fairly simple:

    m.def("mysum", &maths::sum<int>);
    m.def("mysum", &maths::sum<int, int>);
    // ... and so on
    // Just for good measure:)
    m.def("mysum", &maths::sum<int, int, int, int, int, int, int, int, int>);

And later in Python you can just call:

mymodule.mysum(1)
>>> 1

mymodule.mysum(2, 3)
>>> 5

mymodule.mysum(1, 2, 3)) # won't work as there is no overload provided
>>> TypeError: mysum(): incompatible function arguments. The following argument types are supported:
>>>  1. (arg0: int) -> int
>>>  2. (arg0: int, arg1: int) -> int
>>>  3. (arg0: int, arg1: int, arg2: int, arg3: int, arg4: int, arg5: int, arg6: int, arg7: int, arg8: int) -> int

mym…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Mayuresh16
Comment options

Answer selected by Mayuresh16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants