Understanding RefCounted

Steven Schveighoffer schveiguy at gmail.com
Wed May 12 13:38:10 UTC 2021


On 5/12/21 3:28 AM, JG wrote:
> Reading the documentation on RefCounted I get the impression that the 
> following can lead to memory errors. Could someone explain exactly how 
> that could happen? I suppose that problem would be the call something to 
> do with front?
> 
> 
> ```
> private struct RefCountedRangeReturnType(R)
> {
>      import std.typecons : RefCounted;
>      private RefCounted!R r;
>      auto empty() { return r.refCountedPayload.empty; }
>      auto front() { return r.refCountedPayload.front; }
>      void popFront() { r.refCountedPayload.popFront; }
>      auto save() { return 
> typeof(this)(RefCounted!R(r.refCountedPayload.save)); }
> }
> 
> auto refCountedRange(R)(R r)
> {
>      import std.typecons : RefCounted;
>      return  RefCountedRangeReturnType!R(RefCounted!R(r));
> }
> ```

You don't need to access refCountedPayload. RefCounted is supposed to be 
like a transparent reference type, and should forward all calls to the 
referenced item.

I don't see how you will get memory errors from your code. Maybe you can 
elaborate why you think that is?

-Steve


More information about the Digitalmars-d-learn mailing list