From 62f30205e5be7e87f1b566ee8395d9363f2ec984 Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Tue, 14 Jun 2016 20:14:17 -0700 Subject: [PATCH] Additional std::array ctor to support const element cases. --- include/span.h | 5 +++++ tests/span_tests.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/span.h b/include/span.h index 676fdf2..3e0ccc9 100644 --- a/include/span.h +++ b/include/span.h @@ -318,6 +318,11 @@ public: {} template + constexpr span(std::array& arr) + : storage_(&arr[0], extent_type()) + {} + + template ::value>> constexpr span(std::array, N>& arr) : storage_(&arr[0], extent_type()) {} diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 81784c3..5c17552 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -400,6 +400,7 @@ SUITE(span_tests) take_a_span(get_an_array()); } #endif + { auto get_an_array = []() -> std::array { return { 1, 2, 3, 4 }; }; auto take_a_span = [](span s) { static_cast(s); }; @@ -445,6 +446,40 @@ SUITE(span_tests) #endif } + TEST(from_std_array_const_constructor) + { + std::array arr = {1, 2, 3, 4}; + + { + span s{arr}; + CHECK(s.size() == narrow_cast(arr.size()) && s.data() == arr.data()); + } + + { + span s{arr}; + CHECK(s.size() == narrow_cast(arr.size()) && s.data() == arr.data()); + } +#ifdef CONFIRM_COMPILATION_ERRORS + { + span s{arr}; + CHECK(s.size() == 2 && s.data() == arr.data()); + } + + { + span s{arr}; + CHECK(s.size() == 0 && s.data() == arr.data()); + } + + { + span s{arr}; + } + + { + span s{arr}; + } +#endif + } + TEST(from_container_constructor) { std::vector v = {1, 2, 3};