pybind11使用教程笔记__2_C++函数重载

1. 函数重载介绍

If you have a C++14 compatible compiler [2], you can use an alternative syntax to cast the overloaded function:

py::class_<Pet>(m, "Pet")
    .def("set", py::overload_cast<int>(&Pet::set), "Set the pet's age")
    .def("set", py::overload_cast<const std::string &>(&Pet::set), "Set the pet's name");

py::overload_cast 只需要制定输入参数的类型,函数的返回值类型会自动推断,省去了很多麻烦

.def("foo_mutable", py::overload_cast<int, float>(&Widget::foo))
Here, py::overload_cast only requires the parameter types to be specified. The return type and class are deduced.
This avoids the additional noise of void (Pet:😗)() as seen in the raw cast.

If a function is overloaded based on constness, the py::const_ tag should be used:

struct Widget {
    int foo(int x, float y);
    int foo(int x, float y) const;
};
py::class_<Widget>(m, "Widget")
   .def("foo_mutable", py::overload_cast<int, float>(&Widget::foo))
   .def("foo_const",   py::overload_cast<int, float>(&Widget::foo, py::const_));

2. 完整代码

  • struct_pet.h
#include<iostream>

struct Pet{
	Pet(const std::string &name_, int age_){
	    name = name_;
	    age = age_;
	}

	void set(int age_){
		age = age_;
	}

	void set(const std::string &name_){
		name = name_;
	}

	std::string name;
	int age;
};
  • struct_pet_warp.cpp
#include<iostream>
#include <pybind11/pybind11.h>
#include "struct_pet.h"

namespace py = pybind11;


PYBIND11_MODULE(struct_pet, m){
	py::class_<Pet>(m, "Pet", py::dynamic_attr() )
    	.def(py::init<const std::string &, int>() )
		.def("set", py::overload_cast<int>(&Pet::set), "set pet's age")
		.def("set", py::overload_cast<const std::string &>(&Pet::set), "set pet's name")
		.def("__repr__", [](const Pet &a){return "<struct_pet.Pet name: "+a.name+">";})
		.def_readwrite("name", &Pet::name)
		.def_readwrite("age", &Pet::age);

}
  • test_struct.py
from struct_pet import Pet
pet = Pet("tbc", 23)
print(pet.name, pet.age )


pet.set(10)
print(pet.name, pet.age )

pet.set("dindings")
print(pet.name, pet.age )

result:

tbc 23
tbc 10
dindings 10

ref:

https://pybind11.readthedocs.io/en/stable/classes.html#overloaded-methods

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pybind11是一个用于Python和C之间接口转换的轻量级库,它可以为现有的C代码创建Python接口绑定。 在pybind11中,绑定重载指的是支持在Python中使用相同名称但参数类型和数量不同的多个C++函数。这意味着可以根据传递给函数的参数类型和数量来自动选择正确的C++函数进行调用。 例如,可以使用pybind11的`def`函数来绑定重载函数,如下所示: ```cpp #include <pybind11/pybind11.h> void foo(int x) { // do something } void foo(double x) { // do something else } namespace py = pybind11; PYBIND11_MODULE(example, m) { m.def("foo", py::overload_cast<int>(&foo)); m.def("foo", py::overload_cast<double>(&foo)); } ``` 在上面的代码中,`def`函数被用来绑定两个重载函数`foo`,分别接受`int`和`double`类型的参数。当在Python中调用`foo`函数时,根据传递给函数的参数类型自动选择正确的C++函数进行调用。 总结起来,pybind11可以通过使用`def`函数来绑定多个名称相同但参数类型和数量不同的C++函数,从而实现函数重载。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pybind11使用教程笔记__2_C++函数重载](https://blog.csdn.net/weixin_41521681/article/details/106200017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [pybind11.tar](https://download.csdn.net/download/TracelessLe/13204469)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [pybind11学习 | 类的绑定](https://blog.csdn.net/qq_39784672/article/details/128427222)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值