I have some problems understanding the reference counting code in std.stdio. The reduced code my questions refer to is here: https://gist.github.com/1099229 (ignore the not-working struct default constructor, that's just to simplify the code)
in line 5: why is 'refs' initialized to 'uint.max / 2'? in line 40: what does 'swap(p, rhs.p);' do? Does it decrease the reference count of the 'original' File and increase the reference count of the 'new' File? Or does the compiler automatically call the copy constructor and the destructor when it calls opAssign? There was some discussion some time ago whether atomic ops have to be used for ref-counting (something about finalizers of heap allocated structs?). What happened to that issue? How's ref-counting supposed to behave with global variables? -------------------------- module test; File file; void main() { file = File("/blah"); test(); } void test() { file.read(); //<-- will/should this work? } -------------------------- What happens if a ref-counted struct is used as a member in a class? Will the GC decrease the struct's reference count when the class gets collected? Is it possible to add explicit de-referencing to allow a global struct / struct in class to be manually dereferenced? Is detach meant to do that? Would this naive implementation work? https://gist.github.com/1099360 Maybe with the detach addition the copy-ctor and opAssign should throw on p == null? So that copies of a detached object cannot be made? -- Johannes Pfau