From 0fbde488b24f62b4bbbde216647941dcac65c81a Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Sun, 29 Mar 2020 16:08:45 -0700 Subject: Support conversion between Spans of compatible types --- src/span.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/span.h') diff --git a/src/span.h b/src/span.h index 9379b15c81..a7e0020b9a 100644 --- a/src/span.h +++ b/src/span.h @@ -25,6 +25,23 @@ public: constexpr Span(C* data, std::ptrdiff_t size) noexcept : m_data(data), m_size(size) {} constexpr Span(C* data, C* end) noexcept : m_data(data), m_size(end - data) {} + /** Implicit conversion of spans between compatible types. + * + * Specifically, if a pointer to an array of type O can be implicitly converted to a pointer to an array of type + * C, then permit implicit conversion of Span to Span. This matches the behavior of the corresponding + * C++20 std::span constructor. + * + * For example this means that a Span can be converted into a Span. + */ + template ::value, int>::type = 0> + constexpr Span(const Span& other) noexcept : m_data(other.m_data), m_size(other.m_size) {} + + /** Default copy constructor. */ + constexpr Span(const Span&) noexcept = default; + + /** Default assignment operator. */ + Span& operator=(const Span& other) noexcept = default; + constexpr C* data() const noexcept { return m_data; } constexpr C* begin() const noexcept { return m_data; } constexpr C* end() const noexcept { return m_data + m_size; } @@ -44,6 +61,8 @@ public: friend constexpr bool operator<=(const Span& a, const Span& b) noexcept { return !(b < a); } friend constexpr bool operator>(const Span& a, const Span& b) noexcept { return (b < a); } friend constexpr bool operator>=(const Span& a, const Span& b) noexcept { return !(a < b); } + + template friend class Span; }; /** Create a span to a container exposing data() and size(). -- cgit v1.2.3