4 ms·
Why would you respond to a comment about a topic you don't understand and spread false information? You clearly cannot tell the difference between garbage colle
by halayli 4y ago
Why would you respond to a comment about a topic you don't understand and spread false information? You clearly cannot tell the difference between garbage collected language and smart pointers.
- Joker_vD 4y agoAh, so reference counting is not considered a form of garbage collection any more? Ingenious but somewhat of dishonest.
- spacechild1 4y agoSmart pointers do not necessarily use reference counting. std::shared_ptr is reference counted, but std::unique_ptr is not. Needless to say that std::unique_ptr should always be the default choice and std::shared_ptr only used when actually needed.
- kps 4y agostd::unique_ptr is reference counted, it's just that the count only goes up to BOOL_MAX instead of LONG_MAX.
- jb1991 4y agoI think you’re making a joke, but reference counting is different than what a unique pointer does. (a destructor is not the same thing as a reference count, even if that reference count is limited as you suggest.)
- deleted 4y ago[deleted]
- Joker_vD 4y agoNo, I don't think it's a joke. It's just that when your counter is limited to values of 0 and 1 only, you can optimize it so heavily it almost looks like a different thing entirely: binary semaphores are usually called mutexes, but they're still semaphores.
- kps 4y agoIt was a joke and not a joke, a joke with a kernel of truth. But I only now just discovered that C23 is genuinely adding `BOOL_MAX`, which reminds me of the classic Fortran — > The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.
- jb1991 4y agoBut it’s not counted, that’s my point. It’s getting quite pedantic to talk about ones and zeros in this context anyway.
- spacechild1 4y agoPlease 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.
- halayli 4y agolike the other comment said, std::unique_ptr is not ref counted. Ref counting is one of the ways to implement garbage collection but it is not garbage collection by itself. C++ does not have garbage collection.
- pebal 4y agoC++ does not have garbage collection in the standard library, just as shared_ptr was not part of the standard library in the past. You can currently have optional garbage collection in C++: https://github.com/pebal/sgcl https://github.com/pebal/sgcl
- jb1991 4y agoC++ can do pretty much anything, but that doesn't mean that it’s typical to do so. In practice, you will never see a garbage collector in C++.