View source code
							
							
						
								Display the source code in std/algorithm/mutation.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.
							
						Function std.algorithm.mutation.swap
Swaps lhs and rhs. The instances lhs and rhs are moved in
memory, without ever calling opAssign, nor any other function. T
need not be assignable at all to be swapped.
						
				void swap(T)
				(
				
				  ref T lhs,
				
				  ref T rhs
				
				) pure nothrow @nogc @trusted
				
				if (isBlitAssignable!T && !is(typeof(lhs
				
				void swap(T)
				(
				
				  ref T lhs,
				
				  ref T rhs
				
				)
				
				if (is(typeof(lhs
				If lhs and rhs reference the same instance, then nothing is done.
lhs and rhs must be mutable. If T is a struct or union, then
its fields must also all be (recursively) mutable.
Parameters
| Name | Description | 
|---|---|
| lhs | Data to be swapped with rhs. | 
| rhs | Data to be swapped with lhs. | 
Example
// Swapping POD (plain old data) types:
int a = 42, b = 34;
swap(a, b);
assert(a == 34 && b == 42);
// Swapping structs with indirection:
static struct S { int x; char c; int[] y; }
S s1 = { 0, 'z', [ 1, 2 ] };
S s2 = { 42, 'a', [ 4, 6 ] };
swap(s1, s2);
writeln(s1Example
// Non-copyable types can still be swapped.
static struct NoCopy
{
    this(this) { assert(0); }
    int n;
    string s;
}
NoCopy nc1, nc2;
nc1Authors
License
					Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.