diff --git a/gsl/span b/gsl/span index 3a3fb52..0925344 100644 --- a/gsl/span +++ b/gsl/span @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef _MSC_VER @@ -386,6 +387,12 @@ public: { } + template> + constexpr span(const std::unique_ptr& ptr, index_type count) : storage_(ptr.get(), count) {} + + constexpr span(const std::unique_ptr& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) {} + constexpr span(const std::shared_ptr& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) {} + // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement // on Container to be a contiguous sequence container. template (4); + + { + span s{ptr}; + CHECK(s.length() == 1 && s.data() == ptr.get()); + CHECK(s[0] == 4); + } + } + + { + auto ptr = std::unique_ptr{nullptr}; + + { + span s{ptr}; + CHECK(s.length() == 0 && s.data() == nullptr); + } + } + + { + auto arr = std::make_unique(4); + + for (auto i = 0; i < 4; i++) + arr[i] = i + 1; + + { + span s{arr, 4}; + CHECK(s.length() == 4 && s.data() == arr.get()); + CHECK(s[0] == 1 && s[1] == 2); + } + } + + { + auto ptr = std::unique_ptr{nullptr}; + + { + span s{ptr, 0}; + CHECK(s.length() == 0 && s.data() == nullptr); + } + } + } + + TEST(from_shared_pointer_construction) + { + { + auto ptr = std::make_shared(4); + + { + span s{ptr}; + CHECK(s.length() == 1 && s.data() == ptr.get()); + CHECK(s[0] == 4); + } + } + + { + auto ptr = std::shared_ptr{nullptr}; + + { + span s{ptr}; + CHECK(s.length() == 0 && s.data() == nullptr); + } + } + } + TEST(from_container_constructor) { std::vector v = {1, 2, 3};