生成pybind11绑定的模板元函数 我试图用pybDun11创建一些C++函数的Python绑定。函数是模板化的,但在python绑定中,我需要一个模板参数作为函数参数。目前我的方法是对每个模板参数进行大量重复 enum MyEnum {E1, E2, E3}; template<typename T, MyEnum E> returnType templFunction(int arg){ <function body> } PYBIND11_MODULE(moduleName, m){ m.def("pyFunction1", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam1, E1>(arg); case MyEnum::E2: return templFunction<templParam1, E2>(arg); case MyEnum::E3: return templFunction<templParam1, E3>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); m.def("pyFunction2", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam2, E1>(arg); case MyEnum::E2: return templFunction<templParam2, E2>(arg); case MyEnum::E3: return templFunction<templParam2, E3>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); // for template type temlParam3, only E1 and E2 are valid enums m.def("pyFunction3", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam3, E1>(arg); case MyEnum::E2: return templFunction<templParam3, E2>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); // more specializations like above }; enum MyEnum{E1,E2,E3}; 模板 returnType模板函数(int arg){ } PYBIND11_模块(模块名称,m){ m、 def(“pyFunction1”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); case MyEnum::E3:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); m、 def(“pyFunction2”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); case MyEnum::E3:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); //对于模板类型temlParam3,只有E1和E2是有效的枚举 m、 def(“pyFunction3”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); //更多像上面这样的专业 };

生成pybind11绑定的模板元函数 我试图用pybDun11创建一些C++函数的Python绑定。函数是模板化的,但在python绑定中,我需要一个模板参数作为函数参数。目前我的方法是对每个模板参数进行大量重复 enum MyEnum {E1, E2, E3}; template<typename T, MyEnum E> returnType templFunction(int arg){ <function body> } PYBIND11_MODULE(moduleName, m){ m.def("pyFunction1", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam1, E1>(arg); case MyEnum::E2: return templFunction<templParam1, E2>(arg); case MyEnum::E3: return templFunction<templParam1, E3>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); m.def("pyFunction2", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam2, E1>(arg); case MyEnum::E2: return templFunction<templParam2, E2>(arg); case MyEnum::E3: return templFunction<templParam2, E3>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); // for template type temlParam3, only E1 and E2 are valid enums m.def("pyFunction3", [](int arg, MyEnum e){ switch(e){ case MyEnum::E1: return templFunction<templParam3, E1>(arg); case MyEnum::E2: return templFunction<templParam3, E2>(arg); default: throw std::invalid_argument("Unknown enum type ..."); } }); // more specializations like above }; enum MyEnum{E1,E2,E3}; 模板 returnType模板函数(int arg){ } PYBIND11_模块(模块名称,m){ m、 def(“pyFunction1”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); case MyEnum::E3:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); m、 def(“pyFunction2”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); case MyEnum::E3:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); //对于模板类型temlParam3,只有E1和E2是有效的枚举 m、 def(“pyFunction3”,[](int-arg,MyEnum e){ 开关(e){ case MyEnum::E1:返回模板函数(arg); case MyEnum::E2:返回模板函数(arg); 默认值:throw std::无效的_参数(“未知枚举类型…”); } }); //更多像上面这样的专业 };,c++,templates,template-meta-programming,pybind11,C++,Templates,Template Meta Programming,Pybind11,有没有一个好方法可以生成一个高阶函数,这样我就不需要那么多重复的代码 我还有其他具有类似签名和模板参数的函数。因此,理想情况下,我希望将一个函数句柄(或函数名)和一个模板参数传递给一个“元函数”,该“元函数”可以基于enum和给定的模板参数生成专门化 注意:我在每个模板类型(templateparam1,templateParam2,…)上都有一个静态属性,用于确定该模板参数允许的枚举类型。例如,templParam1::allowedEnumTypes=std::vector{E1,E2,E3

有没有一个好方法可以生成一个高阶函数,这样我就不需要那么多重复的代码

我还有其他具有类似签名和模板参数的函数。因此,理想情况下,我希望将一个函数句柄(或函数名)和一个模板参数传递给一个“元函数”,该“元函数”可以基于enum和给定的模板参数生成专门化

注意:我在每个模板类型(
templateparam1
templateParam2
,…)上都有一个静态属性,用于确定该模板参数允许的枚举类型。例如,
templParam1::allowedEnumTypes=std::vector{E1,E2,E3}

您可以编写

template<typename T, MyEnum... Cases>
return_type switcher(int arg, MyEnum e)
{
    return_type r;
    bool tests[] = {(e == Cases ? (r = templFunction<T, Cases>(arg), true) : false)...};
    for(auto t : tests)
        if(t)
            return r;
    throw std::invalid_argument("Unknown enum type ..."); 
}
模板
返回类型切换器(int arg,MyEnum e)
{
返回类型r;
bool测试[]={(e==Cases?(r=templefunction(arg),true):false)…};
用于(自动测试:测试)
if(t)
返回r;
抛出std::无效的_参数(“未知枚举类型…”);
}
并用作

m.def("pyFunction1", switcher<templParam1, E1, E2, E3>);
m.def("pyFunction2", switcher<templParam2, E1, E2, E3>);
m.def("pyFunction3", switcher<templParam3, E1, E2>);
m.def(“pyFunction1”,切换器);
m、 def(“pyFunction2”,开关);
m、 def(“pyFunction3”,开关);

bool数组的可能重复项可能与仅使用一个返回类型的方法相同:
bool done=false;((e==Cases?r=f(arg),done |=true:false),…)
(甚至创建lambda而不是三元运算符)@Jarod42我最初编写了一个折叠表达式,但后来决定为c++11编写。无论哪种方式我都会有一个数组,所以最好使用它。@Passenger这是一个很好的解决方案,适合我使用!是否也可以在function
TemplateFunction
上为开关设置模板?我有几个具有相同签名和模板类型的函数,因此最好重用
switcher
函数。@Bilentor您可以将函数放入一个类中,将其作为另一个模板参数传递,并像
Fn{}.template operator()(arg)