在开始之前,我是C ++ 11的完全菜鸟,几年前就使用过C。
我正在尝试使用pybind11编写C ++ 11代码的python绑定,并遇到主题错误。我基本上是遵循Nvidia的guide,并被卡在这个错误上。 有什么好心的人能指出我正确的方向吗?
定义:
template<int zoom_factor>
class UpSamplePlugin: public nvinfer1::IPluginExt
{
public:
UpSamplePlugin() {}
// Create the plugin at runtime from a byte stream.
UpSamplePlugin(const void* buffer, size_t size)
{
assert(size == sizeof(mInputDims)); // assert datatype of input
mInputDims = *reinterpret_cast<const nvinfer1::Dims*>(buffer);
}
...
}
致电:
py::class_<UpSamplePlugin, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;
错误:
/media/.../plugin/pyUpSample.cpp: In function ‘void pybind11_init_upsampleplugin(pybind11::module&)’:
/media/.../plugin/pyUpSample.cpp:13:90: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr’
py::class_<UpSamplePlugin, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
^
/media/.../plugin/pyUpSample.cpp:13:90: note: expected a type, got ‘UpSamplePlugin’
答案 0 :(得分:1)
没有称为UpSamplePlugin
的类型,这只是一个模板。
因此,您必须执行类似UpSamplePlugin<T>
的操作。您的情况应该是UpSamplePlugin<zoom_factor>
尝试以下代码, ,如果此声明位于模板内:
py::class_<UpSamplePlugin<zoom_factor>, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;
编译器将“创建”对应于UpSamplePlugin<zoom_factor>
的新类型。
如果它不在模板内:
创建另一个模板(可以是模板函数),可以使用zoom_factor将该模板调用为任何常量类型:
template<int zoom_factor>
void doSomething() {
py::class_<UpSamplePlugin<zoom_factor>, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;
}
然后您可以使用任何 已知的时间 zoom_factor