jbcoe / propagate_const

A const-propagating member-pointer-wrapper for the C++ standard library
MIT License
20 stars 8 forks source link

No reset member function #31

Closed pepsiman closed 1 year ago

pepsiman commented 3 years ago

When I tried to use propagate_const in my project, I noticed that propagate_const doesn't have a reset member function. I prefer reset() over = nullptr to clear a unique_ptr.

Could a reset member function be added to the proposal?

jbcoe commented 3 years ago

I'm interested to hear how you are using propagate_const and what you are using it for.

Raw pointer does not have reset hence propagate_const does not have reset. There's no fundamental reason it could not be added. Why do you prefer 'reset'?

pepsiman commented 3 years ago

I looked at propagate_const because SonarQube was reporting that some member functions should be const and I didn't agree.

I'm using propagate_const<unique_ptr<T>> as a class member for pimpl types, recursive types, types using composition of an optional value, and as a value type in maps - absl::flat_hash_map<U, propagate_const<unique_ptr<T>>>. I'm using propagate_const<T*> as a class member for a pointer to the object's parent or siblings. I'm using propagate_const<unique_ptr<T[]>> as a class member for a fixed size array with bounds only known at runtime. I tried using libstdc++'s std::experimental::propagate_const, but it didn't work for array types. For some of these uses, a value_ptr, https://hackernoon.com/value-ptr-the-missing-c-smart-pointer-1f515664153e, might be preferable if the class is intended to be copyable.

For std::string, I prefer clear() over = "" to avoid strlen(""). For std::vector, I would only consider clear(). So for consistency, for std::unique_ptr I prefer reset() over = nullptr. For types with a private constructor, std::make_unique cannot be used, so I prefer reset(new T()) over = unique_ptr<T>(new T()) because it doesn't repeat the type, but maybe I should consider using = absl::WrapUnique(new T())

I'm having issues trying to replace boost::indirected_range<const vector<unique_ptr<T>>> with boost::indirected_range<const vector<propagate_const<unique_ptr<T>>>>

jbcoe commented 3 years ago

You could look at indirect_value

It's newer than propagate_const, less general-purpose but probably more suited for what you need.

Do let me know how it works either way!

pepsiman commented 3 years ago

Yeah, indirect_value looks similar to value_ptr.