用于enable_if_t>的Python绑定
我需要绑定 set() 的乐趣,其定义如下:
template<typename U=T> std::enable_if_t<!is_same_v<U, void>> set(U const& val)
{
set_value(val);
}
template<bool _Cond, typename _Tp=void>
using enable_if_t = typename enable_if<_Cond, _Tp>::type;
我尝试绑定返回类型 (std::enable_if_t<>):
void bind_enable_if_t(py::module &m)
{
py::class<std::enable_if_t<bool, OutputDataType> pyClass(m, "enable_if_t", py::module_local());
}
我收到错误消息:
In function 'void bind_enable_if_t(pybind11:module&)':
error: type/value mismatch at argument 1 in template parameter list for 'template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type'
note: expected a constant of type 'bool', got 'bool'
“期望一个常量”的含义是什么类型为“bool”,有“bool”吗? 进行绑定的正确方法是什么?
感谢您的任何建议。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,如果您有 SFINAE 修饰方法,则可以帮助编译器解析通过
或不通过
&Class:: template
部分进行绑定的正确方法(如果它是独立函数)。在您的情况下,由于
set
对于void
类型完全禁用,我还会将.def
放入if constexpr(! is_same_v)
守卫。Generally, if you have an SFINAE decorated method, you can help the compiler resolve the correct method to bind via
and without the
&Class:: template
part if its a free standing function.In your case, since
set
is completely disabled forvoid
types, I would furthermore put the.def
in anif constexpr(!is_same_v<U, void>)
guard.