Skip to content

Commit 5cbc4a6

Browse files
committed
super special size()
1 parent 9df4a81 commit 5cbc4a6

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

slice.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ namespace iter {
1717
//template <typename Container, typename DifferenceType>
1818
//Slice<Container> slice(Container &&);
1919

20+
template <typename Container>
21+
size_t size(Container & container) {
22+
return container.size();
23+
}
24+
template <typename T, size_t N>
25+
size_t size(T (&)[N]) {
26+
return N;
27+
}
28+
2029
template <typename Container, typename DifferenceType>
2130
class Slice : public IterBase<Container>{
2231
private:
@@ -49,8 +58,8 @@ namespace iter {
4958
(start > stop && step >=0)){
5059
this->stop = start;
5160
}
52-
if (this->stop > static_cast<DifferenceType>(container.size())) {
53-
this->stop = static_cast<DifferenceType>(container.size());
61+
if (this->stop > static_cast<DifferenceType>(size(container))) {
62+
this->stop = static_cast<DifferenceType>(size(container));
5463
}
5564
if (this->start < 0) {
5665
this->start = 0;

tests/testslice.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,10 @@ int main() {
5757
//invalid range returns two begin iters
5858
std::cout << i << std::endl;
5959
}
60-
60+
std::cout<< "\nstatic array[1:8:2]\n";
61+
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
62+
for (auto i : iter::slice(arr,1,8,2)) {
63+
//invalid range returns two begin iters
64+
std::cout << i << std::endl;
65+
}
6166
}

0 commit comments

Comments
 (0)