Skip to content

move semantics #2

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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