简体   繁体   English

在 C++ 库 Gandiva 中使用指向 std::shared_ptr 的指针的目的是什么

[英]What's the purpose of using pointer to std::shared_ptr in C++ library Gandiva

I'm learning the Gandiva module in Apache Arrow.我正在 Apache Arrow 中学习 Gandiva 模块。 I found that many APIs require parameters in the form of std::shared_ptr<T>* , eg here is an typical API:我发现许多 API 需要std::shared_ptr<T>*形式的参数,例如这里是一个典型的 API:

static inline Status Make(SchemaPtr schema, ConditionPtr condition, std::shared_ptr<Filter> *filter)

I don't understand why it uses a pointer to a shared_ptr instead of a simple shared_ptr .我不明白为什么它使用指向shared_ptr的指针而不是简单的shared_ptr To my understanding, a raw pointer should be avoided in C++ as much as possible, and shared_ptr is designed as an alternative to using raw pointers.以我的理解,在C++中应该尽量避免使用原始指针,而shared_ptr被设计为使用原始指针的替代方案。

This is a way for the API to "return" a shared_ptr to you.这是 API 向您“返回” shared_ptr的一种方式。 I have not used this library, but here is an example of what an API like this might do:我没有使用过这个库,但这里有一个 API 可能会做什么的例子:

static inline Status Make(SchemaPtr schema, ConditionPtr condition, std::shared_ptr<Filter> *filter) {
   // ...more stuff here...

   // Assign a new instance of shared_ptr to the variable that the caller passed in
   *filter = std::make_shared<Filter>(...);
   return Status{"ok"};

}

This is often called an "out" parameter and it is documented as such in the API you linked.这通常称为“out”参数,它在您链接的 API 中记录为这样。

There are alternatives.还有其他选择。

  1. The function can use references, but those can disguise the fact that out parameters are being used. function 可以使用引用,但这些可以掩盖正在使用 out 参数的事实。 Make(a, b, c) looks like passing in three normal parameters but Make(a, b, &c) can tip someone off that out parameters are being used. Make(a, b, c)看起来像传递三个正常参数,但Make(a, b, &c)可以提示某人正在使用 out 参数。 This is one reason it is common to prefer pointers over references for out parameters.这是对 out 参数更喜欢指针而不是引用的原因之一。
  2. The function could return a std::tuple<Status, std::shared_ptr<Filter>> instead. function 可以返回std::tuple<Status, std::shared_ptr<Filter>>代替。

Sometimes when using the raw pointer argument, the library may check if the pointer is null and not assign anything to it in that case.有时在使用原始指针参数时,库可能会检查指针是否为 null 并且在这种情况下不会为其分配任何内容。 That would make the out parameter optional, which neither of these alternatives can handle.这将使 out 参数成为可选参数,而这些替代方案都无法处理。 (here is an example of an API using the optional out parameter with a raw pointer: https://doc.qt.io/qt-6/qstring.html#toInt ) (这里是一个 API 的例子,使用带有原始指针的可选输出参数: https://doc.qt.io/qt-6/qstring.html#toInt

The general rule in modern c++ is to avoid using raw pointers when you want to control ownership.现代 c++ 中的一般规则是在您想要控制所有权时避免使用原始指针。 This example does not take ownership of the passed in pointer and so does not break that rule.此示例不获取传入指针的所有权,因此不违反该规则。

Some people conform to a stricter rule where they try to avoid pointers everywhere, but that is less popular.有些人遵守更严格的规则,他们试图避免到处使用指针,但这不太受欢迎。

This is their code style convention for output parameters.这是他们针对 output 参数的代码风格约定。

filter[out] the returned filter object filter[out]返回的过滤器 object

Usage用法

std::shared_ptr<Filter> filter;
Filter::Make(..., &filter);

I would use我会用

Status Make(..., std::shared_ptr<Filter> &filter);

std::shared_ptr<Filter> filter;
Filter::Make(..., filter);

Perhaps seeing the usage &filter they know filter is an output argument w/o looking at the function declaration.也许看到用法&filter他们知道filter是一个 output 参数,而没有查看 function 声明。

Apache Arrow Development Guidelines - Code Style, Linting, and CI Apache Arrow 开发指南 - 代码风格、Linting 和 CI

  • We prefer pointers for output and input/output parameters (the style guide recommends mutable references in some cases).我们更喜欢 output 和输入/输出参数的指针(风格指南在某些情况下建议使用可变引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM