Add span construction from unique_ptr and shared_ptr

This patch adds support for std::unique_ptr and
std::shared_ptr to the gsl::span
class instead of having to manually grab the pointer via
get().

For reference, this is part of the following issue:
https://github.com/Microsoft/GSL/issues/402
This commit is contained in:
Rian Quinn
2016-11-03 19:55:41 -06:00
committed by Neil MacIntosh
parent d641796b21
commit 2b51b8767a
2 changed files with 73 additions and 0 deletions

View File

@ -29,6 +29,7 @@
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <memory>
#ifdef _MSC_VER
@ -386,6 +387,12 @@ public:
{
}
template<class ArrayElementType = std::add_pointer<element_type>>
constexpr span(const std::unique_ptr<ArrayElementType>& ptr, index_type count) : storage_(ptr.get(), count) {}
constexpr span(const std::unique_ptr<ElementType>& ptr) : storage_(ptr.get(), ptr.get() ? 1 : 0) {}
constexpr span(const std::shared_ptr<ElementType>& 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 <class Container,