Tightened SFINAE for span<U> to span<T> conversions.
This commit is contained in:
1 parent
ba8ebef509
commit
c94a66f468
2 files changed
+104
-96
No files matched your search
+57
-34
@@ -77,6 +77,11 @@ template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
|
||||
class span;
|
||||
|
||||
|
||||
// [views.constants], constants
|
||||
constexpr const std::ptrdiff_t dynamic_extent = -1;
|
||||
|
||||
|
||||
// implementation details
|
||||
namespace details
|
||||
{
|
||||
template <class T>
|
||||
@@ -96,45 +101,55 @@ struct is_span : is_span_oracle<std::remove_cv_t<T>>
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_pointer_conversion
|
||||
: std::integral_constant<bool,
|
||||
std::is_pointer<From>::value &&
|
||||
std::is_pointer<To>::value &&
|
||||
std::is_convertible<From, To>::value
|
||||
: std::bool_constant<
|
||||
std::is_pointer<From>::value &&
|
||||
std::is_pointer<To>::value &&
|
||||
std::is_convertible<From, To>::value
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_integral_conversion
|
||||
: std::integral_constant<bool,
|
||||
std::is_integral<From>::value &&
|
||||
std::is_integral<To>::value &&
|
||||
sizeof(From) == sizeof(To) &&
|
||||
alignof(From) == alignof(To) &&
|
||||
std::is_convertible<From, To>::value
|
||||
: std::bool_constant<
|
||||
std::is_integral<From>::value &&
|
||||
std::is_integral<To>::value &&
|
||||
sizeof(From) == sizeof(To) &&
|
||||
alignof(From) == alignof(To) &&
|
||||
std::is_convertible<From, To>::value
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template <std::ptrdiff_t From, std::ptrdiff_t To>
|
||||
struct is_allowed_extent_conversion
|
||||
: std::bool_constant<
|
||||
From == To ||
|
||||
From == gsl::dynamic_extent ||
|
||||
To == gsl::dynamic_extent
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From, class To>
|
||||
struct is_allowed_element_type_conversion
|
||||
: std::integral_constant<bool,
|
||||
std::is_same<From, std::remove_cv_t<To>>::value ||
|
||||
is_allowed_pointer_conversion<From, To>::value ||
|
||||
is_allowed_integral_conversion<From, To>::value
|
||||
: std::bool_constant<
|
||||
std::is_same<From, std::remove_cv_t<To>>::value ||
|
||||
is_allowed_pointer_conversion<From, To>::value ||
|
||||
is_allowed_integral_conversion<From, To>::value
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From>
|
||||
struct is_allowed_element_type_conversion<From, byte>
|
||||
: std::integral_constant<bool, !std::is_const<From>::value>
|
||||
: std::bool_constant<!std::is_const<From>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template <class From>
|
||||
struct is_allowed_element_type_conversion<From, const byte>
|
||||
: std::integral_constant<bool, true>
|
||||
: std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
@@ -191,7 +206,7 @@ public:
|
||||
auto ret{*this};
|
||||
return ret += n;
|
||||
}
|
||||
|
||||
|
||||
span_iterator& operator+=(difference_type n) noexcept
|
||||
{
|
||||
index_ += n;
|
||||
@@ -217,10 +232,14 @@ public:
|
||||
}
|
||||
|
||||
reference operator[](difference_type n) const noexcept
|
||||
{ return *(*this + n); }
|
||||
{
|
||||
return *(*this + n);
|
||||
}
|
||||
|
||||
bool operator==(const span_iterator& rhs) const noexcept
|
||||
{ return span_ == rhs.span_ && index_ == rhs.index_; }
|
||||
{
|
||||
return span_ == rhs.span_ && index_ == rhs.index_;
|
||||
}
|
||||
|
||||
bool operator!=(const span_iterator& rhs) const noexcept { return !(*this == rhs); }
|
||||
|
||||
@@ -231,9 +250,9 @@ public:
|
||||
}
|
||||
|
||||
bool operator<=(const span_iterator& rhs) const noexcept { return !(rhs < *this); }
|
||||
|
||||
|
||||
bool operator>(const span_iterator& rhs) const noexcept { return rhs < *this; }
|
||||
|
||||
|
||||
bool operator>=(const span_iterator& rhs) const noexcept { return !(rhs > *this); }
|
||||
|
||||
void swap(span_iterator& rhs) noexcept
|
||||
@@ -250,21 +269,20 @@ private:
|
||||
template <typename Span>
|
||||
span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
|
||||
const span_iterator<Span>& rhs) noexcept
|
||||
{ return rhs + n; }
|
||||
{
|
||||
return rhs + n;
|
||||
}
|
||||
|
||||
template <typename Span>
|
||||
span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
|
||||
const span_iterator<Span>& rhs) noexcept
|
||||
{ return rhs - n; }
|
||||
|
||||
{
|
||||
return rhs - n;
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
|
||||
|
||||
// [views.constants], constants
|
||||
constexpr const std::ptrdiff_t dynamic_extent = -1;
|
||||
|
||||
|
||||
// [span], class template span
|
||||
template <class ElementType, std::ptrdiff_t Extent>
|
||||
class span {
|
||||
@@ -330,21 +348,23 @@ public:
|
||||
constexpr span(span&& other) noexcept = default;
|
||||
|
||||
template <class OtherElementType, std::ptrdiff_t OtherExtent,
|
||||
class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value &&
|
||||
class = std::enable_if_t<
|
||||
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
|
||||
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
|
||||
>
|
||||
>
|
||||
constexpr span(const span<OtherElementType, OtherExtent>& other)
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), other.length())
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
|
||||
{}
|
||||
|
||||
template <class OtherElementType, std::ptrdiff_t OtherExtent,
|
||||
class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value &&
|
||||
class = std::enable_if_t<
|
||||
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
|
||||
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
|
||||
>
|
||||
>
|
||||
constexpr span(span<OtherElementType, OtherExtent>&& other)
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), other.length())
|
||||
: storage_(reinterpret_cast<pointer>(other.data()), extent_type<OtherExtent>(other.size()))
|
||||
{}
|
||||
|
||||
~span() noexcept = default;
|
||||
@@ -418,6 +438,8 @@ public:
|
||||
reverse_iterator rend() const noexcept { return {this, 0}; }
|
||||
|
||||
private:
|
||||
constexpr static const bool is_span_type = true;
|
||||
|
||||
template <index_type Extent>
|
||||
class extent_type;
|
||||
|
||||
@@ -430,10 +452,11 @@ private:
|
||||
constexpr extent_type() noexcept {}
|
||||
|
||||
template <index_type Other>
|
||||
constexpr extent_type(extent_type<Other>) noexcept
|
||||
constexpr extent_type(extent_type<Other> ext) noexcept
|
||||
{
|
||||
static_assert(Other == Extent,
|
||||
static_assert(Other == Extent || Other == dynamic_extent,
|
||||
"Mismatch between fixed-size extent and size of initializing data.");
|
||||
Expects(ext.size() == Extent);
|
||||
}
|
||||
|
||||
constexpr extent_type(index_type size)
|
||||
|
||||
Reference in new issue
Block a user