7 ms·
Please show me the counter in std::unique_ptr :-)
by spacechild1 4y ago
Please show me the counter in std::unique_ptr :-)
- Joker_vD 4y agoSince that counter can only have two values, it's storage is optimized and merged with the underlying raw pointer itself (not unlike Rust's null pointer optimization for enums): non-null value of the underlying pointer also serve to represent the counter's value of 1, and the null value serves to represent the counter's value of 0. The check to not under-decrement, for example, manifests as the null check before calling the deleter on the underlying pointer.
- spacechild1 4y agoSigh, I saw this coming. By this definition, every C++ object with a (non-trivial) destructor would be "reference counted". Sorry, but I am not interested in playing word games.
- jb1991 4y agoI can’t upvote this comment enough. The amount of pedantic nonsense on HN these days is disappointing. I’m not sure why I even bother to read comments here anymore.
- Joker_vD 4y agoNope. Only those with "if (this->inner_resource) { /* delete inner resource */ }" inside the destructor could count, others wouldn't. Edit: actually, nevermind. You are (almost) right: all objects with automatic storage duration and with non-trivial destructors are, very pedantically speaking, reference counted; but objects with dynamic storage duration are not. So RAII works by leveraging the built-in very primitive and restricted (binary) form of reference counting and builds on top of it to be able to reference-count objects with dynamic storage duration as well.
- spacechild1 4y agoThis is just petty sophism. I won't participate further. Have a nice day.