Skip to content

Commit

Permalink
preview::span is now intercompatible with std::span (#51)
Browse files Browse the repository at this point in the history
* Add constructor/conversion operator to std::span

* Update span.cc
  • Loading branch information
lackhole authored Sep 29, 2024
1 parent ef42814 commit e6db00b
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 4 deletions.
67 changes: 63 additions & 4 deletions include/preview/__span/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <type_traits>

#include "preview/__core/nodiscard.h"
#include "preview/__core/std_version.h"
#include "preview/__iterator/basic_const_iterator.h"
#include "preview/__iterator/contiguous_iterator.h"
#include "preview/__iterator/iter_reference_t.h"
Expand All @@ -32,6 +33,10 @@
#include "preview/__type_traits/remove_cvref.h"
#include "preview/__type_traits/type_identity.h"

#if PREVIEW_CXX_VERSION >= 20
#include <span>
#endif

namespace preview {
namespace detail {

Expand Down Expand Up @@ -277,23 +282,25 @@ class span : private detail::span_storage_t<T, Extent> {
// explicit(extent != std::dynamic_extent && N == std::dynamic_extent)
// constexpr span( const std::span<U, N>& source ) noexcept;
template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<!(Extent != dynamic_extent && OtherExtent == dynamic_extent)>, // explicit(false)
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(false)
bool_constant<!(Extent != dynamic_extent && OtherExtent == dynamic_extent)>
>::value, int> = 0>
constexpr span(const span<U, OtherExtent>& source) noexcept
: base(source.data(), source.size()) {}

template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<(Extent != dynamic_extent && OtherExtent == dynamic_extent)>, // explicit(true)
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(true)
bool_constant<(Extent != dynamic_extent && OtherExtent == dynamic_extent)>
>::value, int> = 0>
constexpr explicit span(const span<U, OtherExtent>& source) noexcept
: base(source.data(), source.size()) {}
Expand All @@ -302,6 +309,58 @@ class span : private detail::span_storage_t<T, Extent> {
// constexpr span( const span& other ) noexcept = default;
constexpr span(const span& other) noexcept = default;

#if PREVIEW_CXX_VERSION >= 20
template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(true)
bool_constant<(Extent != dynamic_extent && OtherExtent == dynamic_extent)>
>::value, int> = 0>
constexpr explicit span(const std::span<U, OtherExtent>& source) noexcept
: base(source.data(), source.size()) {}

template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(false)
bool_constant<!(Extent != dynamic_extent && OtherExtent == dynamic_extent)>
>::value, int> = 0>
constexpr span(const std::span<U, OtherExtent>& source) noexcept
: base(source.data(), source.size()) {}

template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(true)
bool_constant<(OtherExtent != dynamic_extent && Extent == dynamic_extent)>
>::value, int> = 0>
constexpr explicit operator std::span<U, OtherExtent>() const noexcept {
return std::span<U, OtherExtent>(data(), size());
}

template<typename U, std::size_t OtherExtent, std::enable_if_t<conjunction<
bool_constant<(
Extent == dynamic_extent ||
OtherExtent == dynamic_extent ||
Extent == OtherExtent)>,
std::is_convertible<U(*)[], element_type(*)[]>,
// explicit(false)
bool_constant<!(OtherExtent != dynamic_extent && Extent == dynamic_extent)>
>::value, int> = 0>
constexpr operator std::span<U, OtherExtent>() const noexcept {
return std::span<U, OtherExtent>(data(), size());
}
#endif

constexpr iterator begin() const noexcept { return iterator{data()}; }
constexpr iterator end() const noexcept { return iterator{data() + size()}; }

Expand Down
95 changes: 95 additions & 0 deletions test/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "preview/algorithm.h"
#include "preview/array.h"
#include "preview/ranges.h"

namespace ranges = preview::ranges;
Expand Down Expand Up @@ -114,3 +115,97 @@ TEST(VERSIONED(Span), deduction_guides) {
}

#endif // C++17

#if PREVIEW_CXX_VERSION >= 20

#include <span>

TEST(VERSIONED(Span), std_span_compat) {
const auto arr = preview::to_array({1, 2, 3, 4, 5});

static_assert(std::is_convertible_v<preview::span<int>, preview::span<int>> ==
std::is_convertible_v<preview::span<int>, std::span<int>>, "");
static_assert(std::is_convertible_v<preview::span<unsigned int>, preview::span<int>> ==
std::is_convertible_v<preview::span<unsigned int>, std::span<int>>, "");
static_assert(std::is_convertible_v<preview::span<int>, preview::span<unsigned int>> ==
std::is_convertible_v<preview::span<int>, std::span<unsigned int>>, "");

static_assert(std::is_convertible_v<preview::span<int, 5>, preview::span<int>> ==
std::is_convertible_v<preview::span<int, 5>, std::span<int>>, "");
static_assert(std::is_convertible_v<preview::span<unsigned int, 5>, preview::span<int>> ==
std::is_convertible_v<preview::span<unsigned int, 5>, std::span<int>>, "");
static_assert(std::is_convertible_v<preview::span<int, 5>, preview::span<unsigned int>> ==
std::is_convertible_v<preview::span<int, 5>, std::span<unsigned int>>, "");

static_assert(std::is_convertible_v<preview::span<int>, preview::span<int, 5>> ==
std::is_convertible_v<preview::span<int>, std::span<int, 5>>, "");
static_assert(std::is_convertible_v<preview::span<unsigned int>, preview::span<int, 5>> ==
std::is_convertible_v<preview::span<unsigned int>, std::span<int, 5>>, "");
static_assert(std::is_convertible_v<preview::span<int>, preview::span<unsigned int, 5>> ==
std::is_convertible_v<preview::span<int>, std::span<unsigned int, 5>>, "");

static_assert(std::is_convertible_v<preview::span<int, 5>, preview::span<int, 5>> ==
std::is_convertible_v<preview::span<int, 5>, std::span<int, 5>>, "");
static_assert(std::is_convertible_v<preview::span<unsigned int, 5>, preview::span<int, 5>> ==
std::is_convertible_v<preview::span<unsigned int, 5>, std::span<int, 5>>, "");
static_assert(std::is_convertible_v<preview::span<int, 5>, preview::span<unsigned int, 5>> ==
std::is_convertible_v<preview::span<int, 5>, std::span<unsigned int, 5>>, "");

static_assert(preview::is_explicitly_convertible_v<preview::span<int>, preview::span<int>> ==
preview::is_explicitly_convertible_v<preview::span<int>, std::span<int>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<unsigned int>, preview::span<int>> ==
preview::is_explicitly_convertible_v<preview::span<unsigned int>, std::span<int>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<int>, preview::span<unsigned int>> ==
preview::is_explicitly_convertible_v<preview::span<int>, std::span<unsigned int>>, "");

static_assert(preview::is_explicitly_convertible_v<preview::span<int, 5>, preview::span<int>> ==
preview::is_explicitly_convertible_v<preview::span<int, 5>, std::span<int>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<unsigned int, 5>, preview::span<int>> ==
preview::is_explicitly_convertible_v<preview::span<unsigned int, 5>, std::span<int>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<int, 5>, preview::span<unsigned int>> ==
preview::is_explicitly_convertible_v<preview::span<int, 5>, std::span<unsigned int>>, "");

static_assert(preview::is_explicitly_convertible_v<preview::span<int>, preview::span<int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<int>, std::span<int, 5>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<unsigned int>, preview::span<int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<unsigned int>, std::span<int, 5>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<int>, preview::span<unsigned int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<int>, std::span<unsigned int, 5>>, "");

static_assert(preview::is_explicitly_convertible_v<preview::span<int, 5>, preview::span<int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<int, 5>, std::span<int, 5>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<unsigned int, 5>, preview::span<int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<unsigned int, 5>, std::span<int, 5>>, "");
static_assert(preview::is_explicitly_convertible_v<preview::span<int, 5>, preview::span<unsigned int, 5>> ==
preview::is_explicitly_convertible_v<preview::span<int, 5>, std::span<unsigned int, 5>>, "");

static_assert(std::is_constructible_v<preview::span<int>, preview::span<int>> ==
std::is_constructible_v< std::span<int>, preview::span<int>>, "");
static_assert(std::is_constructible_v<preview::span<unsigned int>, preview::span<int>> ==
std::is_constructible_v< std::span<unsigned int>, preview::span<int>>, "");
static_assert(std::is_constructible_v<preview::span<int>, preview::span<unsigned int>> ==
std::is_constructible_v< std::span<int>, preview::span<unsigned int>>, "");

static_assert(std::is_constructible_v<preview::span<int, 5>, preview::span<int>> ==
std::is_constructible_v< std::span<int, 5>, preview::span<int>>, "");
static_assert(std::is_constructible_v<preview::span<unsigned int, 5>, preview::span<int>> ==
std::is_constructible_v< std::span<unsigned int, 5>, preview::span<int>>, "");
static_assert(std::is_constructible_v<preview::span<int, 5>, preview::span<unsigned int>> ==
std::is_constructible_v< std::span<int, 5>, preview::span<unsigned int>>, "");

static_assert(std::is_constructible_v<preview::span<int>, preview::span<int, 5>> ==
std::is_constructible_v< std::span<int>, preview::span<int, 5>>, "");
static_assert(std::is_constructible_v<preview::span<unsigned int>, preview::span<int, 5>> ==
std::is_constructible_v< std::span<unsigned int>, preview::span<int, 5>>, "");
static_assert(std::is_constructible_v<preview::span<int>, preview::span<unsigned int, 5>> ==
std::is_constructible_v< std::span<int>, preview::span<unsigned int, 5>>, "");

static_assert(std::is_constructible_v<preview::span<int, 5>, preview::span<int, 5>> ==
std::is_constructible_v< std::span<int, 5>, preview::span<int, 5>>, "");
static_assert(std::is_constructible_v<preview::span<unsigned int, 5>, preview::span<int, 5>> ==
std::is_constructible_v< std::span<unsigned int, 5>, preview::span<int, 5>>, "");
static_assert(std::is_constructible_v<preview::span<int, 5>, preview::span<unsigned int, 5>> ==
std::is_constructible_v< std::span<int, 5>, preview::span<unsigned int, 5>>, "");
}

#endif // C++17

0 comments on commit e6db00b

Please sign in to comment.