I'm starting to think it would be both useful and feasible to have a general
reference counting template struct in Phobos:

struct RefCounted(T, alias onZero) {
    // Implementation
}

Basically, you would provide it with a type T that it's supposed to count
references to and an action (a function that takes a T and returns void)
that's supposed to happen when the amount of references drop to zero.

Reference counting is a pretty useful general technique for managing resources
that can't have cycles and I think it makes sense to have a generic template
for it instead of having people reimplement the same code over and over for
file handles, large arrays of primitives that they allocate on the C heap and
want freed deterministically, database connections, etc.  Does this sound
worthwhile?

Reply via email to