mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-01 19:21:28 +02:00
Replace MakeSpan helper with Span deduction guide
This commit is contained in:
20
src/span.h
20
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
|
||||
|
Reference in New Issue
Block a user