From 9d8866a732a9c2da79079fbd73bb2b933a6af5af Mon Sep 17 00:00:00 2001 From: ericLemanissier Date: Mon, 9 May 2016 13:02:27 +0200 Subject: [PATCH 1/2] gsl::at overload for initializer_list initializer_list do not have subscript operator, so the generic container overload of gsl::at fails to compile. This commits adds an overload of gsl::at for initializer_lists, using *(initializer_list::begin()+index) instead of subscript operator --- include/gsl_util.h | 4 ++++ tests/at_tests.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) 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 *[]) From b94a220c4ad78749d4fc9c8a53d4b0cd48e27820 Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Fri, 13 May 2016 11:50:25 -0700 Subject: [PATCH 2/2] Corrected typo identified in #288. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5743bb3..35e93e4 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ of your GSL source. These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`. -1. Create a directory to contain the build outputs for a particular architecture (we name it c:\GSL\vs14-x86 in this example). +1. Create a directory to contain the build outputs for a particular architecture (we name it c:\GSL\build-x86 in this example). cd GSL md build-x86