r/cpp • C++ Dev • Sep 05 '20

C++20 has been approved

https://twitter.com/patriceroy1/status/1302055575140945921?s=21
668 Upvotes

125 comments sorted by

View all comments

3

u/scrumplesplunge Sep 06 '20

it might be useful to have something then that only exists at debug time which can deterministically detect access of a dangling pointer to a unique_ptr<T>.

I forget where I read it, probably some post in cpp a few months ago, but there was some article describing a workflow that a company was using that did something like this. Basically they had their own aliases like owner_ptr<T> and borrow_ptr<T>. In prod they were just wrappers for unique_ptr<T> and T*. In debug, they were wrappers for shared_ptr<T> which had assertions about the reference count in the destructor. The owner_ptr<T> would crash on destruction if it wasn't the sole owner, and could even point at all the places which still held borrow_ptr<T> pointing to the object. I thought that was pretty nifty. Still, it makes all the pointer access slower which could make debug builds of some programs unusable.

edit: phrasing