Skip to content

move semantics #2

@ufcpp

Description

@ufcpp
interface INonCopyable { }

interface IMovable : INonCopyable
{
    // void OnMoved(); ?
}

static class MoveExtensions
{
    public static T Move<T>(ref this T x)
        where T : struct, IMovable
    {
        // needs interlocked exchange?
        var t = x;
        // x.OnMoved(); ?
        x = default;
        return t;
    }
}
  • allow var y = x.Move();
  • disallow to use x after Move

This is not so hard for local variables and parameters but fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions