diff --git a/include/gsl_util.h b/include/gsl_util.h index edfcce4..b42b66a 100644 --- a/include/gsl_util.h +++ b/include/gsl_util.h @@ -128,6 +128,10 @@ template constexpr typename Cont::value_type& at(Cont& cont, size_t index) { Expects(index < cont.size()); return cont[index]; } +template +constexpr const T& at(std::initializer_list cont, size_t index) +{ Expects(index < cont.size()); return *(cont.begin() + index); } + } // namespace gsl diff --git a/tests/at_tests.cpp b/tests/at_tests.cpp index d27dd9d..1a9f814 100644 --- a/tests/at_tests.cpp +++ b/tests/at_tests.cpp @@ -17,6 +17,7 @@ #include #include #include +#include using namespace std; using namespace gsl; @@ -52,6 +53,16 @@ SUITE(at_tests) CHECK_THROW(at(a, 4), fail_fast); } + + TEST(InitializerList) + { + std::initializer_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 *[])