Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize Usage / Interface for raw and smart pointer usage #43

Open
IanTheEngineer opened this issue Apr 26, 2016 · 3 comments
Open

Comments

@IanTheEngineer
Copy link
Member

Right now pointers are both raw and shared. We should choose one (preferably shared) and stick with it. Additionally, we should consistently return shared pointers rather than passing them in by reference and populating them.

@MatthewPeterKelly
Copy link
Contributor

MatthewPeterKelly commented May 22, 2018

There are a few cases of interest here. In both cases I suggest that we follow the google style guide for C++.

  1. Reference arguments: google style guide: reference arguments
  • function inputs are either pass by value of const reference
  • function outputs are passed by raw pointer
  1. Object lifetime management: google style guide: smart pointers
  • keep ownership local if possible
  • if ownership is transfered, make it explicit by using unique_ptr
  • case unique_ptr to shared_ptr only if absolutely necessary (prefer to move the unique_ptr)
  1. Follow Resource Acquisition Is Initialization (RAII)
  • avoid design patterns that require init() to be called on an object in order for it to "work"
  • use a factory method to return a unique pointer to the object. If the constructor failed then the unique pointer is null.

@IanTheEngineer IanTheEngineer changed the title Standardize Usage / Interface for raw and shared_ptr usage Standardize Usage / Interface for raw and smart pointer usage May 22, 2018
@IanTheEngineer
Copy link
Member Author

IanTheEngineer commented May 22, 2018

I agree with 2 and 3. However, on reading google's doc on reference arguments, I don't see a good justification for function outputs being passed by raw pointer. Who is responsible for cleaning up the object?

@MatthewPeterKelly
Copy link
Contributor

I believe that the caller is responsible for managing the object lifetime. This method of handling output arguments is also suggested by the ROS style guide.

If the function is creating an object with a non-trivial lifetime, then we should return a unique pointer to the object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants