diff --git a/include/gsl.h b/include/gsl.h index 95116dc..7f883f6 100644 --- a/include/gsl.h +++ b/include/gsl.h @@ -89,7 +89,13 @@ template T& at(std::array& arr, size_t index) { fail_fast_assert(index < N); return arr[index]; } template -typename Cont::value_type& at(Cont& cont, size_t index) { fail_fast_assert(index < cont.size()); return cont[index]; } +typename Cont::value_type& at(Cont& cont, size_t index) +{ + fail_fast_assert(index < cont.size()); + auto it = cont.begin(); + std::advance(it, index); + return *it; +} // diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index 6a86307..49b6b01 100644 --- a/tests/at_tests.cpp +++ b/tests/at_tests.cpp @@ -17,6 +17,7 @@ #include #include #include +#include using namespace std; using namespace Guide; @@ -52,6 +53,16 @@ SUITE(at_tests) CHECK_THROW(at(a, 4), fail_fast); } + + TEST(StdList) + { + std::list a = { 1, 2, 3, 4 }; + + for (int i = 0; i < 4; ++i) + CHECK(at(a, i) == i+1); + + CHECK_THROW(at(a, 4), fail_fast); + } } int main(int, const char *[])