View source code
Display the source code in std/typecons.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.typecons.RefCounted/refCounted - multiple declarations

Function refCounted

Like safeRefCounted but used to initialize RefCounted instead. Intended for backwards compatibility, otherwise it is preferable to use safeRefCounted.

RefCounted!(T,RefCountedAutoInitialize.no) refCounted(T) (
  T val
);

Example

static struct File
{
    static size_t nDestroyed;
    string name;
    @disable this(this); // not copyable
    ~this() { name = null; ++nDestroyed; }
}

auto file = File("name");
writeln(file.name); // "name"
static assert(!__traits(compiles, {auto file2 = file;}));
writeln(File.nDestroyed); // 0

{
    import std.algorithm.mutation : move;
    auto rcFile = refCounted(move(file));
    writeln(rcFile.name); // "name"
    writeln(File.nDestroyed); // 1
    writeln(file.name); // null

    auto rcFile2 = rcFile;
    writeln(rcFile.refCountedStore.refCount); // 2
    writeln(File.nDestroyed); // 1
}

writeln(File.nDestroyed); // 2

Struct RefCounted

The old version of SafeRefCounted, before borrow existed. Old code may be relying on @safety of some of the member functions which cannot be safe in the new scheme, and can avoid breakage by continuing to use this. SafeRefCounted should be preferred, as this type is outdated and unrecommended for new code.

struct RefCounted(T, RefCountedAutoInitialize autoInit = RefCountedAutoInitialize.yes) ;

Example

auto rc1 = RefCounted!int(5);
writeln(rc1); // 5
auto rc2 = rc1;
rc2 = 42;
writeln(rc1); // 42

Authors

Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara

License

Boost License 1.0.