diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2020-03-29 16:08:45 -0700 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2020-04-11 02:15:25 -0700 |
commit | 0fbde488b24f62b4bbbde216647941dcac65c81a (patch) | |
tree | 1130a22d0ffe0d26d34b219e3b966b271b8bc56a /src | |
parent | 7cbfebbf3df0d26f518811e0bfb7abf270c83e37 (diff) | |
download | bitcoin-0fbde488b24f62b4bbbde216647941dcac65c81a.tar.xz |
Support conversion between Spans of compatible types
Diffstat (limited to 'src')
-rw-r--r-- | src/span.h | 19 |
1 files changed, 19 insertions, 0 deletions
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<O> to Span<C>. This matches the behavior of the corresponding + * C++20 std::span constructor. + * + * For example this means that a Span<T> can be converted into a Span<const T>. + */ + template <typename O, typename std::enable_if<std::is_convertible<O (*)[], C (*)[]>::value, int>::type = 0> + constexpr Span(const Span<O>& 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 <typename O> friend class Span; }; /** Create a span to a container exposing data() and size(). |