From 5aabf85d2c2075f912b6ffb6f90ff9583f20bab1 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 24 Oct 2020 19:57:51 +0300 Subject: libcamera: span: Make constructors explicit as required by C++20 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The C++20 std::span class, after which Span is modelled, specifies four constructors as explicit when extent is not dynamic_extent. Align our implementation with those requirements. A careful reviewer may notice that this change addresses five constructors, not four. The reason is that the two constructors taking Container and const Container parameters are not specified in C++20, which uses a single constructor taking a range parameter instead. As ranges are not available in C++17, the Container constructors are our best effort at providing a similar feature. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- include/libcamera/span.h | 74 ++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/include/libcamera/span.h b/include/libcamera/span.h index c0e43992..91e9f974 100644 --- a/include/libcamera/span.h +++ b/include/libcamera/span.h @@ -113,12 +113,12 @@ public: { } - constexpr Span(pointer ptr, [[maybe_unused]] size_type count) + explicit constexpr Span(pointer ptr, [[maybe_unused]] size_type count) : data_(ptr) { } - constexpr Span(pointer first, [[maybe_unused]] pointer last) + explicit constexpr Span(pointer first, [[maybe_unused]] pointer last) : data_(first) { } @@ -154,35 +154,35 @@ public: } template - constexpr Span(Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) + explicit constexpr Span(Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) : data_(utils::data(cont)) { } template - constexpr Span(const Container &cont, - std::enable_if_t::value && - !details::is_array::value && - !std::is_array::value && - std::is_convertible (*)[], - element_type (*)[]>::value, - std::nullptr_t> = nullptr) + explicit constexpr Span(const Container &cont, + std::enable_if_t::value && + !details::is_array::value && + !std::is_array::value && + std::is_convertible (*)[], + element_type (*)[]>::value, + std::nullptr_t> = nullptr) : data_(utils::data(cont)) { static_assert(utils::size(cont) == Extent, "Size mismatch"); } template - constexpr Span(const Span &s, - std::enable_if_t::value && - N == Extent, - std::nullptr_t> = nullptr) noexcept + explicit constexpr Span(const Span &s, + std::enable_if_t::value && + N == Extent, + std::nullptr_t> = nullptr) noexcept : data_(s.data()) { } @@ -212,24 +212,24 @@ public: constexpr Span first() const { static_assert(Count <= Extent, "Count larger than size"); - return { data(), Count }; + return Span{ data(), Count }; } constexpr Span first(std::size_t Count) const { - return { data(), Count }; + return Span{ data(), Count }; } template constexpr Span last() const { static_assert(Count <= Extent, "Count larger than size"); - return { data() + size() - Count, Count }; + return Span{ data() + size() - Count, Count }; } constexpr Span last(std::size_t Count) const { - return { data() + size() - Count, Count }; + return Span{ data() + size() - Count, Count }; } template @@ -238,13 +238,19 @@ public: static_assert(Offset <= Extent, "Offset larger than size"); static_assert(Count == dynamic_extent || Count + Offset <= Extent, "Offset + Count larger than size"); - return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; } constexpr Span subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const { - return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; } private: @@ -371,7 +377,7 @@ public: template constexpr Span first() const { - return { data(), Count }; + return Span{ data(), Count }; } constexpr Span first(std::size_t Count) const @@ -382,24 +388,30 @@ public: template constexpr Span last() const { - return { data() + size() - Count, Count }; + return Span{ data() + size() - Count, Count }; } constexpr Span last(std::size_t Count) const { - return { data() + size() - Count, Count }; + return Span{ data() + size() - Count, Count }; } template constexpr Span subspan() const { - return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; } constexpr Span subspan(std::size_t Offset, std::size_t Count = dynamic_extent) const { - return { data() + Offset, Count == dynamic_extent ? size() - Offset : Count }; + return Span{ + data() + Offset, + Count == dynamic_extent ? size() - Offset : Count + }; } private: -- cgit v1.2.1