From f61a9bba48fa410e2df4519793f7480be4b85934 Mon Sep 17 00:00:00 2001 From: Neil MacIntosh Date: Mon, 29 Feb 2016 13:16:48 -0800 Subject: [PATCH] Added array constructors. --- include/span.h | 17 +++++++++---- tests/span_tests.cpp | 57 +++++++++----------------------------------- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/include/span.h b/include/span.h index 4983295..2d95899 100644 --- a/include/span.h +++ b/include/span.h @@ -108,14 +108,21 @@ public: {} template - constexpr span(element_type(&arr)[N]) : storage_(&arr[0], extent_type()) + constexpr span(element_type(&arr)[N]) + : storage_(&arr[0], extent_type()) + {} + + template + constexpr span(std::array, N>& arr) + : storage_(&arr[0], extent_type()) + {} + + template + constexpr span(const std::array, N>& arr) + : storage_(&arr[0], extent_type()) {} #if 0 // TODO - template - constexpr span(array, N>& arr); - template - constexpr span(const array, N>& arr); template constexpr span(Container& cont); template diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index e4bdef4..e13694c 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -306,7 +306,7 @@ SUITE(span_tests) } #endif { - span s{ arr2d[0] }; + span s{ &(arr2d[0]), 1 }; CHECK(s.length() == 1 && s.data() == &arr2d[0]); } @@ -335,34 +335,18 @@ SUITE(span_tests) } #endif { - //span s{arr3d[0]}; - //CHECK(s.length() == 1 && s.data() == &arr3d[0]); + span s{&arr3d[0], 1}; + CHECK(s.length() == 1 && s.data() == &arr3d[0]); } } -#if 0 + TEST(from_dynamic_array_constructor) { double(*arr)[3][4] = new double[100][3][4]; { - span s(arr, 10); - CHECK(s.length() == 120 && s.data() == &arr[0][0][0]); - CHECK_THROW(s[10][3][4], fail_fast); - } - - { - span s(arr, 10); - CHECK(s.length() == 120 && s.data() == &arr[0][0][0]); - } - - { - span s(arr, 10); - CHECK(s.length() == 120 && s.data() == &arr[0][0][0]); - } - - { - span s(arr, 0); - CHECK(s.length() == 0 && s.data() == &arr[0][0][0]); + span s(&arr[0][0][0], 10); + CHECK(s.length() == 10 && s.data() == &arr[0][0][0]); } delete[] arr; @@ -388,6 +372,7 @@ SUITE(span_tests) CHECK(cs.size() == narrow_cast(arr.size()) && cs.data() == arr.data()); } +#ifdef CONFIRM_COMPILATION_ERRORS { span s{arr}; CHECK(s.size() == 2 && s.data() == arr.data()); @@ -404,27 +389,17 @@ SUITE(span_tests) CHECK(cs.size() == 0 && cs.data() == arr.data()); } - // TODO This is currently an unsupported scenario. We will come back to it as we revise - // the multidimensional interface and what transformations between dimensionality look like - //{ - // span s{arr}; - // CHECK(s.size() == narrow_cast(arr.size()) && s.data() == arr.data()); - //} - { -#ifdef CONFIRM_COMPILATION_ERRORS span s{arr}; -#endif } { -#ifdef CONFIRM_COMPILATION_ERRORS auto get_an_array = []() { return std::array{1, 2, 3, 4}; }; auto take_a_span = [](span s) { (void) s; }; // try to take a temporary std::array take_a_span(get_an_array()); -#endif } +#endif } TEST(from_const_std_array_constructor) @@ -440,7 +415,7 @@ SUITE(span_tests) 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()); @@ -451,29 +426,19 @@ SUITE(span_tests) CHECK(s.size() == 0 && s.data() == arr.data()); } - // TODO This is currently an unsupported scenario. We will come back to it as we revise - // the multidimensional interface and what transformations between dimensionality look like - //{ - // span s{arr}; - // CHECK(s.size() == narrow_cast(arr.size()) && s.data() == arr.data()); - //} - { -#ifdef CONFIRM_COMPILATION_ERRORS span s{arr}; -#endif } { -#ifdef CONFIRM_COMPILATION_ERRORS auto get_an_array = []() -> const std::array { return {1, 2, 3, 4}; }; auto take_a_span = [](span s) { (void) s; }; // try to take a temporary std::array take_a_span(get_an_array()); -#endif } +#endif } - +#if 0 TEST(from_container_constructor) { std::vector v = {1, 2, 3};