-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.hpp
More file actions
22 lines (16 loc) · 712 Bytes
/
slice.hpp
File metadata and controls
22 lines (16 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <array>
template<size_t START, size_t END, class T, size_t ORIG>
std::array<T,END-START>& slice( std::array<T,ORIG>& arr) {
static_assert( END <= ORIG
&& START < ORIG,
"slice(array): array index out of bounds");
return *reinterpret_cast<std::array<T,END-START>*>(&arr[START]);
}
template<size_t LEN, class T, size_t ORIG>
std::array<T,LEN> slice( std::array<T,ORIG> arr, size_t start) {
if( start + LEN >= ORIG )
throw std::range_error("slice: array index out of bounds");
if( start < 0 )
throw std::range_error("slice: array index out of bounds");
return *reinterpret_cast<std::array<T,LEN>*>(&arr[start]);
}