【发布时间】:2022-01-21 11:03:52
【问题描述】:
将 unique_ptr 与自定义删除器一起用于返回 ptr 的 c 函数非常简单,但我如何将它用于将 ptr 的 ptr 作为参数的函数(如 GError)
我在几个案例中遇到过这种情况,但没有找到直接的方法来做到这一点。我错过了什么吗?
using Element = std::unique_ptr<GstElement, void (*)(GstElement*)>;
using Error = std::unique_ptr<GError, void (*)(GError*)>;
...
GError* plain_err;
Element pipeline(gst_parse_launch(str, &plain_err), object_unref); // ok
Error uniq_err {nullptr, error_free};
Element pipeline(gst_parse_launch(str, &(uniq_err.get())), object_unref); // error: lvalue required as unary ‘&’ operand
我能想到的唯一方法是稍后分配它
GError* plain_err;
Element pipeline(gst_parse_launch(str, &plain_err), object_unref);
Error uniq_err {plain_err, error_free};
plain_error = nullptr;
【问题讨论】:
-
除非你特别喜欢投反对票,否则不要用 C 标签标记 C++ 问题。
-
@JonathanLeffler 问题是关于在 c++ unique_ptr 中使用来自 c 的普通指针,我仍然只标记 c++ 吗?
-
@tejas 除非您希望您的程序使用 C 编译器进行编译,否则无需使用 [C] 标记。
标签: c++ gstreamer unique-ptr