Search code examples
c++c++11shared-ptrapache-arrow

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


I'm learning the Gandiva module in Apache Arrow. I found that many APIs require parameters in the form of std::shared_ptr<T>*, eg here is an typical 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. 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.


Solution

  • This is their code style convention for output parameters.

    filter[out] the returned filter 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.

    Apache Arrow Development Guidelines - Code Style, Linting, and CI

    • We prefer pointers for output and input/output parameters (the style guide recommends mutable references in some cases).