Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions std/typecons.d
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ template Tuple(Specs...)
}

/**
* Takes a slice of this `Tuple`.
* Takes a slice by-reference of this `Tuple`.
*
* Params:
* from = A `size_t` designating the starting position of the slice.
Expand All @@ -982,9 +982,14 @@ template Tuple(Specs...)
* the original.
*/
@property
ref Tuple!(sliceSpecs!(from, to)) slice(size_t from, size_t to)() @trusted
ref inout(Tuple!(sliceSpecs!(from, to))) slice(size_t from, size_t to)() inout @trusted
if (from <= to && to <= Types.length)
{
static assert(
(typeof(this).alignof % typeof(return).alignof == 0) &&
(expand[from].offsetof % typeof(return).alignof == 0),
"Slicing by reference is impossible because of an alignment mistmatch. (See Phobos issue #15645.)");

return *cast(typeof(return)*) &(field[from]);
}

Expand All @@ -997,6 +1002,10 @@ template Tuple(Specs...)
auto s = a.slice!(1, 3);
static assert(is(typeof(s) == Tuple!(string, float)));
assert(s[0] == "abc" && s[1] == 4.5);

// Phobos issue #15645
Tuple!(int, short, bool, double) b;
static assert(!__traits(compiles, b.slice!(2, 4)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about this code, but isn't the 4 out of bounds? Or are the arguments 1-indexed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the docs:

to = A size_t designating the ending position (exclusive) of the slice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks

}

/**
Expand Down