用于enable_if_t>的Python绑定

发布于 2025-01-10 22:07:10 字数 903 浏览 0 评论 0 原文

我需要绑定 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”吗? 进行绑定的正确方法是什么?

感谢您的任何建议。

I need to bind for a fun of set(), which is defined as below:

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;

I tried to bind for the return 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()); 
}

I got error messages:

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'

What is the meaning of "expected a constant of type 'bool', got 'bool''?
What'll be the correct way to do the binding?

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-01-17 22:07:10

一般来说,如果您有 SFINAE 修饰方法,则可以帮助编译器解析通过

using T = MyConcreteType;

// ...
.def("set", &Class::template set<T>)

或不通过 &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

using T = MyConcreteType;

// ...
.def("set", &Class::template set<T>)

and without the &Class:: template part if its a free standing function.

In your case, since set is completely disabled for void types, I would furthermore put the .def in an if constexpr(!is_same_v<U, void>) guard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文