diff options
author | Pieter Wuille <pieter@wuille.net> | 2021-11-01 16:32:53 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2021-11-29 17:58:53 -0500 |
commit | 568dd2f83900a11a4dbba1250722791a135bf0a9 (patch) | |
tree | b9091ef5f60075570c9f0e0c487282450e152ec0 /src/span.h | |
parent | 383d350bd5107bfe00e3b90a00cab9a3c1397c72 (diff) |
Replace MakeSpan helper with Span deduction guide
Diffstat (limited to 'src/span.h')
-rw-r--r-- | src/span.h | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/span.h b/src/span.h index 746e41f2f4..ce5b7ca316 100644 --- a/src/span.h +++ b/src/span.h @@ -222,13 +222,15 @@ public: template <typename O> friend class Span; }; -// MakeSpan helps constructing a Span of the right type automatically. -/** MakeSpan for arrays: */ -template <typename A, int N> Span<A> constexpr MakeSpan(A (&a)[N]) { return Span<A>(a, N); } -/** MakeSpan for temporaries / rvalue references, only supporting const output. */ -template <typename V> constexpr auto MakeSpan(V&& v SPAN_ATTR_LIFETIMEBOUND) -> typename std::enable_if<!std::is_lvalue_reference<V>::value, Span<const typename std::remove_pointer<decltype(v.data())>::type>>::type { return std::forward<V>(v); } -/** MakeSpan for (lvalue) references, supporting mutable output. */ -template <typename V> constexpr auto MakeSpan(V& v SPAN_ATTR_LIFETIMEBOUND) -> Span<typename std::remove_pointer<decltype(v.data())>::type> { return v; } +// Deduction guides for Span +// For the pointer/size based and iterator based constructor: +template <typename T, typename EndOrSize> Span(T*, EndOrSize) -> Span<T>; +// For the array constructor: +template <typename T, std::size_t N> Span(T (&)[N]) -> Span<T>; +// For the temporaries/rvalue references constructor, only supporting const output. +template <typename T> Span(T&&) -> Span<std::enable_if_t<!std::is_lvalue_reference_v<T>, const std::remove_pointer_t<decltype(std::declval<T&&>().data())>>>; +// For (lvalue) references, supporting mutable output. +template <typename T> Span(T&) -> Span<std::remove_pointer_t<decltype(std::declval<T&>().data())>>; /** Pop the last element off a span, and return a reference to that element. */ template <typename T> @@ -274,7 +276,7 @@ inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_c // Helper function to safely convert a Span to a Span<[const] unsigned char>. template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; } -/** Like MakeSpan, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ -template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(MakeSpan(std::forward<V>(v)))) { return UCharSpanCast(MakeSpan(std::forward<V>(v))); } +/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */ +template <typename V> constexpr auto MakeUCharSpan(V&& v) -> decltype(UCharSpanCast(Span{std::forward<V>(v)})) { return UCharSpanCast(Span{std::forward<V>(v)}); } #endif // BITCOIN_SPAN_H |