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
Additional guidelines for use of shared_ptr #1640
Comments
The use of |
@ryanbartley: As I said, I'm not talking about the general pattern to wrap objects into shared pointers - I'm fine with that. But about situations like the case mentioned above, where a shared pointer is used in an awkward way for a local temporary array, which makes the code both slower and more complex. |
I'd say overall that's a fair critique, though the example you're citing is probably unusually egregious, and is (as you said) quite an old piece of the codebase. In fact that code is older than a A trickier subject is the extent to which All that to say, if you see places this kind of thing could be improved, we're generally amenable and grateful for the contribution. |
Seems like I've overstated / poorly explained my goal. My main question is: Should there be two more points in the section about shared_ptr in
I got the impression that adding those points (the second one would need more work) might be beneficial based on seeing a few cases where those guidelines are being violated (the example was indeed an exceptional extreme case). However, if those cases are in general very rare or only found in old code, then they would only bloat the style guide without adding real value. Also, if you think they would discourage any potential contributors / make life harder for them I agree they would certainly do more harm than good as it is mainly a efficiency / style matter not a correctness issue. |
My 2 cents: For code we write today, I think we try to use So, I'd say both of your points are very good guidelines, and also things we should look to refactoring within the codebase as time permits. |
Seems there is not enought support for / interest in this here, so I'm closing this for now. |
In short, I wonder if there should be a note in the
CONTRIBUTING.md
file that cautions against unnecessary use ofstd::shared_ptr
and encourage the use ofstd::make_shared
when ashared_ptr
is really necessary.So far, I've only had a serious look at a very small part of the code base, so this might be a misconception on my part, but I get the impression that
std::shared_ptr
is somewhat overused.I'm not talking about the practice to wrap almost any object that is passed around into a shared_ptr. I see the advantage that it gives in terms of simplicity, even, if it might not always be strictly necessary and adds some overhead. However, there seem to be also quite a number of usages, where nothing is shared or passed around and a
std::unique_ptr
, astd::vector
or even just a simple member variable would have been the much better option. An extreme case can be seen here, wherestd::vector
was essentially reimplemented using ashared_ptr
- just that shared_ptr to an array with a custom deallocator is an extremely inefficient way to do this due to additional dynamic memory allocation, type erasure and threadsafe reference counting.Admittedly this could be a problem of the past. The example I mentioned above was from 2010, when
std::shared_ptr
wasn't even standardized.The text was updated successfully, but these errors were encountered: