Tightened SFINAE for span<U> to span<T> conversions.

This commit is contained in:
Neil MacIntosh 2016-06-12 18:28:19 -07:00
parent ba8ebef509
commit c94a66f468
2 changed files with 104 additions and 96 deletions

View File

@ -77,6 +77,11 @@ template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
class span; class span;
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
// implementation details
namespace details namespace details
{ {
template <class T> template <class T>
@ -96,45 +101,55 @@ struct is_span : is_span_oracle<std::remove_cv_t<T>>
template <class From, class To> template <class From, class To>
struct is_allowed_pointer_conversion struct is_allowed_pointer_conversion
: std::integral_constant<bool, : std::bool_constant<
std::is_pointer<From>::value && std::is_pointer<From>::value &&
std::is_pointer<To>::value && std::is_pointer<To>::value &&
std::is_convertible<From, To>::value std::is_convertible<From, To>::value
> >
{ {
}; };
template <class From, class To> template <class From, class To>
struct is_allowed_integral_conversion struct is_allowed_integral_conversion
: std::integral_constant<bool, : std::bool_constant<
std::is_integral<From>::value && std::is_integral<From>::value &&
std::is_integral<To>::value && std::is_integral<To>::value &&
sizeof(From) == sizeof(To) && sizeof(From) == sizeof(To) &&
alignof(From) == alignof(To) && alignof(From) == alignof(To) &&
std::is_convertible<From, To>::value 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> template <class From, class To>
struct is_allowed_element_type_conversion struct is_allowed_element_type_conversion
: std::integral_constant<bool, : std::bool_constant<
std::is_same<From, std::remove_cv_t<To>>::value || std::is_same<From, std::remove_cv_t<To>>::value ||
is_allowed_pointer_conversion<From, To>::value || is_allowed_pointer_conversion<From, To>::value ||
is_allowed_integral_conversion<From, To>::value is_allowed_integral_conversion<From, To>::value
> >
{ {
}; };
template <class From> template <class From>
struct is_allowed_element_type_conversion<From, byte> 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> template <class From>
struct is_allowed_element_type_conversion<From, const byte> 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}; auto ret{*this};
return ret += n; return ret += n;
} }
span_iterator& operator+=(difference_type n) noexcept span_iterator& operator+=(difference_type n) noexcept
{ {
index_ += n; index_ += n;
@ -217,10 +232,14 @@ public:
} }
reference operator[](difference_type n) const noexcept reference operator[](difference_type n) const noexcept
{ return *(*this + n); } {
return *(*this + n);
}
bool operator==(const span_iterator& rhs) const noexcept 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); } 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; } 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 void swap(span_iterator& rhs) noexcept
@ -250,21 +269,20 @@ private:
template <typename Span> template <typename Span>
span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n, span_iterator<Span> operator+(typename span_iterator<Span>::difference_type n,
const span_iterator<Span>& rhs) noexcept const span_iterator<Span>& rhs) noexcept
{ return rhs + n; } {
return rhs + n;
}
template <typename Span> template <typename Span>
span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n, span_iterator<Span> operator-(typename span_iterator<Span>::difference_type n,
const span_iterator<Span>& rhs) noexcept const span_iterator<Span>& rhs) noexcept
{ return rhs - n; } {
return rhs - n;
}
} // namespace details } // namespace details
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
// [span], class template span // [span], class template span
template <class ElementType, std::ptrdiff_t Extent> template <class ElementType, std::ptrdiff_t Extent>
class span { class span {
@ -330,21 +348,23 @@ public:
constexpr span(span&& other) noexcept = default; constexpr span(span&& other) noexcept = default;
template <class OtherElementType, std::ptrdiff_t OtherExtent, 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 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
> >
> >
constexpr span(const span<OtherElementType, OtherExtent>& other) 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, 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 details::is_allowed_element_type_conversion<OtherElementType, element_type>::value
> >
> >
constexpr span(span<OtherElementType, OtherExtent>&& other) 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; ~span() noexcept = default;
@ -418,6 +438,8 @@ public:
reverse_iterator rend() const noexcept { return {this, 0}; } reverse_iterator rend() const noexcept { return {this, 0}; }
private: private:
constexpr static const bool is_span_type = true;
template <index_type Extent> template <index_type Extent>
class extent_type; class extent_type;
@ -430,10 +452,11 @@ private:
constexpr extent_type() noexcept {} constexpr extent_type() noexcept {}
template <index_type Other> 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."); "Mismatch between fixed-size extent and size of initializing data.");
Expects(ext.size() == Extent);
} }
constexpr extent_type(index_type size) constexpr extent_type(index_type size)

View File

@ -395,14 +395,14 @@ SUITE(span_tests)
{ {
auto get_an_array = []()->std::array<int, 4> { return{1, 2, 3, 4}; }; auto get_an_array = []()->std::array<int, 4> { return{1, 2, 3, 4}; };
auto take_a_span = [](span<int> s) { (void)s; }; auto take_a_span = [](span<int> s) { static_cast<void>(s); };
// try to take a temporary std::array // try to take a temporary std::array
take_a_span(get_an_array()); take_a_span(get_an_array());
} }
#endif #endif
{ {
auto get_an_array = []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; }; auto get_an_array = []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; };
auto take_a_span = [](span<const int> s) { (void)s; }; auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
// try to take a temporary std::array // try to take a temporary std::array
take_a_span(get_an_array()); take_a_span(get_an_array());
} }
@ -438,7 +438,7 @@ SUITE(span_tests)
{ {
auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; }; auto get_an_array = []() -> const std::array<int, 4> { return {1, 2, 3, 4}; };
auto take_a_span = [](span<const int> s) { (void) s; }; auto take_a_span = [](span<const int> s) { static_cast<void>(s); };
// try to take a temporary std::array // try to take a temporary std::array
take_a_span(get_an_array()); take_a_span(get_an_array());
} }
@ -482,35 +482,35 @@ SUITE(span_tests)
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
auto get_temp_vector = []() -> std::vector<int> { return {}; }; auto get_temp_vector = []() -> std::vector<int> { return {}; };
auto use_span = [](span<int> s) { (void) s; }; auto use_span = [](span<int> s) { static_cast<void>(s); };
use_span(get_temp_vector()); use_span(get_temp_vector());
#endif #endif
} }
{ {
auto get_temp_vector = []() -> std::vector<int> { return{}; }; auto get_temp_vector = []() -> std::vector<int> { return{}; };
auto use_span = [](span<const int> s) { (void)s; }; auto use_span = [](span<const int> s) { static_cast<void>(s); };
use_span(get_temp_vector()); use_span(get_temp_vector());
} }
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
auto get_temp_string = []() -> std::string { return{}; }; auto get_temp_string = []() -> std::string { return{}; };
auto use_span = [](span<char> s) { (void)s; }; auto use_span = [](span<char> s) { static_cast<void>(s); };
use_span(get_temp_string()); use_span(get_temp_string());
#endif #endif
} }
{ {
auto get_temp_string = []() -> std::string { return {}; }; auto get_temp_string = []() -> std::string { return {}; };
auto use_span = [](span<const char> s) { (void) s; }; auto use_span = [](span<const char> s) { static_cast<void>(s); };
use_span(get_temp_string()); use_span(get_temp_string());
} }
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
auto get_temp_vector = []() -> const std::vector<int> { return {}; }; auto get_temp_vector = []() -> const std::vector<int> { return {}; };
auto use_span = [](span<const char> s) { (void) s; }; auto use_span = [](span<const char> s) { static_cast<void>(s); };
use_span(get_temp_vector()); use_span(get_temp_vector());
#endif #endif
} }
@ -518,7 +518,7 @@ SUITE(span_tests)
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
auto get_temp_string = []() -> const std::string { return {}; }; auto get_temp_string = []() -> const std::string { return {}; };
auto use_span = [](span<const char> s) { (void) s; }; auto use_span = [](span<const char> s) { static_cast<void>(s); };
use_span(get_temp_string()); use_span(get_temp_string());
#endif #endif
} }
@ -536,34 +536,34 @@ SUITE(span_tests)
{ {
span<DerivedClass> avd; span<DerivedClass> avd;
span<const DerivedClass> avcd = avd; span<const DerivedClass> avcd = avd;
(void)avcd; static_cast<void>(avcd);
} }
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
span<DerivedClass> avd; span<DerivedClass> avd;
span<BaseClass> avb = avd; span<BaseClass> avb = avd;
(void) avb; static_cast<void>(avb);
#endif #endif
} }
{ {
span<int> s; span<int> s;
span<unsigned int> s2 = s; span<unsigned int> s2 = s;
(void)s2; static_cast<void>(s2);
} }
{ {
span<int> s; span<int> s;
span<const unsigned int> s2 = s; span<const unsigned int> s2 = s;
(void)s2; static_cast<void>(s2);
} }
{ {
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
span<int> s; span<int> s;
span<short> s2 = s; span<short> s2 = s;
(void)s2; static_cast<void>(s2);
#endif #endif
} }
} }
@ -984,72 +984,59 @@ SUITE(span_tests)
} }
} }
#if 0
TEST(fixed_size_conversions) TEST(fixed_size_conversions)
{ {
int arr[] = {1, 2, 3, 4}; int arr[] = {1, 2, 3, 4};
// converting to an span from an equal size array is ok // converting to an span from an equal size array is ok
span<int, 4> av4 = arr; span<int, 4> s4 = arr;
CHECK(av4.length() == 4); CHECK(s4.length() == 4);
// converting to dynamic_range a_v is always ok // converting to dynamic_range is always ok
{ {
span<int, dynamic_range> av = av4; span<int> s = s4;
(void) av; CHECK(s.length() == s4.length());
} static_cast<void>(s);
{
span<int, dynamic_range> av = arr;
(void) av;
} }
// initialization or assignment to static span that REDUCES size is NOT ok // initialization or assignment to static span that REDUCES size is NOT ok
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
{ {
span<int, 2> av2 = arr; span<int, 2> s = arr;
} }
{ {
span<int, 2> av2 = av4; span<int, 2> s2 = s4;
static_cast<void>(s2);
} }
#endif #endif
// even when done dynamically
{ {
span<int, dynamic_range> av = arr; span<int> s = arr;
span<int, 2> av2 = av; auto f = [&]() {
(void) av2; span<int, 2> s2 = s;
} static_cast<void>(s2);
};
#ifdef CONFIRM_COMPILATION_ERRORS CHECK_THROW(f(), fail_fast);
{
span<int, dynamic_range> av = arr;
span<int, 2, 1> av2 = av.as_span(dim<2>(), dim<2>());
}
#endif
{
span<int, dynamic_range> av = arr;
span<int, 2, 1> av2 = as_span(av, dim<>(2), dim<>(2));
auto workaround_macro = [&]() { return av2[{1, 0}] == 2; };
CHECK(workaround_macro());
} }
// but doing so explicitly is ok // but doing so explicitly is ok
// you can convert statically // you can convert statically
{ {
span<int, 2> av2 = {arr, 2}; span<int, 2> s2 = {arr, 2};
(void) av2; static_cast<void>(s2);
} }
{ {
span<int, 1> av2 = av4.first<1>(); span<int, 1> s1 = s4.first<1>();
(void) av2; static_cast<void>(s1);
} }
// ...or dynamically // ...or dynamically
{ {
// NB: implicit conversion to span<int,2> from span<int,dynamic_range> // NB: implicit conversion to span<int,1> from span<int>
span<int, 1> av2 = av4.first(1); span<int, 1> s1 = s4.first(1);
(void) av2; static_cast<void>(s1);
} }
// initialization or assignment to static span that requires size INCREASE is not ok. // initialization or assignment to static span that requires size INCREASE is not ok.
@ -1057,31 +1044,29 @@ SUITE(span_tests)
#ifdef CONFIRM_COMPILATION_ERRORS #ifdef CONFIRM_COMPILATION_ERRORS
{ {
span<int, 4> av4 = arr2; span<int, 4> s3 = arr2;
} }
{ {
span<int, 2> av2 = arr2; span<int, 2> s2 = arr2;
span<int, 4> av4 = av2; span<int, 4> s4a = s2;
} }
#endif #endif
{ {
auto f = [&]() { auto f = [&]() {
span<int, 4> av9 = {arr2, 2}; span<int, 4> s4 = {arr2, 2};
(void) av9; static_cast<void>(s4);
}; };
CHECK_THROW(f(), fail_fast); CHECK_THROW(f(), fail_fast);
} }
// this should fail - we are trying to assign a small dynamic a_v to a fixed_size larger one // this should fail - we are trying to assign a small dynamic span to a fixed_size larger one
span<int, dynamic_range> av = arr2; span<int> av = arr2;
auto f = [&]() { auto f = [&]() {
span<int, 4> av2 = av; span<int, 4> s4 = av;
(void) av2; static_cast<void>(s4);
}; };
CHECK_THROW(f(), fail_fast); CHECK_THROW(f(), fail_fast);
} }
#endif
} }
int main(int, const char* []) { return UnitTest::RunAllTests(); } int main(int, const char* []) { return UnitTest::RunAllTests(); }