Python 在PybDun11中为C++模板类简化生成包装类:在块范围内不能出现模板声明 我试图简化在C++模板类中为PybDun11生成包装类。下面是一个简单的示例,下面的答案显示了问题: #include <pybind11/pybind11.h> #include <iostream> namespace py = pybind11; template<class T> class Foo { public: Foo(T bar) : bar_(bar) {} void print() { std::cout << "Type id: " << typeid(T).name() << '\n'; } private: T bar_; }; PYBIND11_MODULE(example, m) { template<typename T> void declare_foo(py::module &m, std::string &typestr) { using Class = Foo<T>; std::string pyclass_name = std::string("Foo") + typestr; py::class_<Class>(m, pyclass_name.c_str()) .def(py::init< T >()) .def("print", &Class::print); } declare_foo<int>(m, "Int"); declare_foo<double>(m, "Double"); # More similar declarations follow here... }
我得到一个错误:Python 在PybDun11中为C++模板类简化生成包装类:在块范围内不能出现模板声明 我试图简化在C++模板类中为PybDun11生成包装类。下面是一个简单的示例,下面的答案显示了问题: #include <pybind11/pybind11.h> #include <iostream> namespace py = pybind11; template<class T> class Foo { public: Foo(T bar) : bar_(bar) {} void print() { std::cout << "Type id: " << typeid(T).name() << '\n'; } private: T bar_; }; PYBIND11_MODULE(example, m) { template<typename T> void declare_foo(py::module &m, std::string &typestr) { using Class = Foo<T>; std::string pyclass_name = std::string("Foo") + typestr; py::class_<Class>(m, pyclass_name.c_str()) .def(py::init< T >()) .def("print", &Class::print); } declare_foo<int>(m, "Int"); declare_foo<double>(m, "Double"); # More similar declarations follow here... },python,c++,pybind11,Python,C++,Pybind11,我得到一个错误: example.cpp: In function ‘void pybind11_init_example(pybind11::module&)’: example.cpp:18:5: error: a template declaration cannot appear at block scope 18 | template<typename T> | ^~~~~~~~ 我使用宏提出了以下解决方法 template<
example.cpp: In function ‘void pybind11_init_example(pybind11::module&)’:
example.cpp:18:5: error: a template declaration cannot appear at block scope
18 | template<typename T>
| ^~~~~~~~
我使用宏提出了以下解决方法
template<class T>
class Foo {
public:
Foo(T bar) : bar_(bar) {}
void print() {
std::cout << "Type id: " << typeid(T).name() << '\n';
}
private:
T bar_;
};
#define DECLARE_FOO(T, NAME) { \
py::class_<Foo<T> >(m, (std::string("Foo")+NAME).c_str()) \
.def(py::init< T >()) \
.def("print", &Foo<T>::print); \
}
PYBIND11_MODULE(example, m) {
DECLARE_FOO(int, "int");
DECLARE_FOO(float, "float");
}
它似乎在工作,但是我不确定这个宏是否足够健壮。我使用宏想出了以下解决方法
template<class T>
class Foo {
public:
Foo(T bar) : bar_(bar) {}
void print() {
std::cout << "Type id: " << typeid(T).name() << '\n';
}
private:
T bar_;
};
#define DECLARE_FOO(T, NAME) { \
py::class_<Foo<T> >(m, (std::string("Foo")+NAME).c_str()) \
.def(py::init< T >()) \
.def("print", &Foo<T>::print); \
}
PYBIND11_MODULE(example, m) {
DECLARE_FOO(int, "int");
DECLARE_FOO(float, "float");
}
它似乎在工作,但是我不确定这个宏是否足够健壮。类似的错误表明你不能在块范围内有模板声明,你显然是在块范围内。只需将其移到外部并通过常量引用或值捕获字符串参数 将代码更改为 样板 void declare_foopy::module&m,const std::string&typestr{ 使用Class=Foo; std::string pyclass_name=std::stringFoo+typestr; py::class_m,pyclass_name.c_str .defpy::init
不建议依赖可传递的包含。就像错误提示您不能在块范围内有模板声明一样,您显然在块范围内。只需将其移到外部并通过常量引用或值捕获字符串参数 将代码更改为 样板 void declare_foopy::module&m,const std::string&typestr{ 使用Class=Foo; std::string pyclass_name=std::stringFoo+typestr; py::class_m,pyclass_name.c_str .defpy::init