View source code
Display the source code in std/range/primitives.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Enum std.range.primitives.member hasSwappableElements
Returns true
if R
is an input range and has swappable
elements. The following code should compile for any range
with swappable elements.
R r; static assert(isInputRange!R); swap(r.front, r.front); static if (isBidirectionalRange!R) swap(r.back, r.front); static if (isRandomAccessRange!R) swap(r[], r.front);
Declaration
enum hasSwappableElements(R) = isInputRange!R && is(typeof((inout int = 0) { R r = R.init; swap(r.front, r.front); static if (isBidirectionalRange!R) { swap(r.back, r.front); } static if (isRandomAccessRange!R) { swap(r[0], r.front); } } ));
Example
static assert(!hasSwappableElements!(const int[])); static assert(!hasSwappableElements!(const(int)[])); static assert(!hasSwappableElements!(inout(int)[])); static assert( hasSwappableElements!(int[])); static assert(!hasSwappableElements!( string)); static assert(!hasSwappableElements!(dstring)); static assert(!hasSwappableElements!( char[])); static assert( hasSwappableElements!(dchar[]));
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.