Fixed ctors for Container and std::array to accept temporaries.

This commit is contained in:
Neil MacIntosh 2016-03-17 17:20:33 -07:00
parent 717a2e35f1
commit 3d4c34966a
3 changed files with 31 additions and 41 deletions

View File

@ -2159,8 +2159,8 @@ public:
value_type operator[](difference_type n) const noexcept value_type operator[](difference_type n) const noexcept
{ {
return (*m_container)[m_itr[n]]; return (*m_container)[m_itr[n]];
;
} }
bool operator==(const general_span_iterator& rhs) const noexcept bool operator==(const general_span_iterator& rhs) const noexcept
{ {
Expects(m_container == rhs.m_container); Expects(m_container == rhs.m_container);

View File

@ -187,7 +187,7 @@ public:
: storage_(&arr[0], extent_type<N>()) : storage_(&arr[0], extent_type<N>())
{} {}
template <size_t N> template <size_t N, class = std::enable_if_t<is_const<element_type>::value>>
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr)
: storage_(&arr[0], extent_type<N>()) : storage_(&arr[0], extent_type<N>())
{} {}
@ -201,14 +201,13 @@ public:
> >
constexpr span(Container& cont) : span(cont.data(), cont.size()) {} constexpr span(Container& cont) : span(cont.data(), cont.size()) {}
// NB: the SFINAE here uses .data() as an incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container.
template <class Container, template <class Container,
class = std::enable_if_t<!details::is_span<Container>::value && class = std::enable_if_t<std::is_const<element_type>::value &&
!details::is_span<Container>::value &&
std::is_convertible<Container::pointer, pointer>::value && std::is_convertible<Container::pointer, pointer>::value &&
std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value>
> >
span(const Container&&) = delete; constexpr span(const Container& cont) : span(cont.data(), cont.size()) {}
constexpr span(const span& other) noexcept = default; constexpr span(const span& other) noexcept = default;
constexpr span(span&& other) noexcept = default; constexpr span(span&& other) noexcept = default;
@ -232,10 +231,10 @@ public:
{ {
} }
#if 0 // TODO
~span() noexcept = default; ~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default; constexpr span& operator=(const span& other) noexcept = default;
constexpr span& operator=(span&& other) noexcept = default; constexpr span& operator=(span&& other) noexcept = default;
#if 0 // TODO
// [span.sub], span subviews // [span.sub], span subviews
template <ptrdiff_t Count> template <ptrdiff_t Count>

View File

@ -394,12 +394,18 @@ SUITE(span_tests)
} }
{ {
auto get_an_array = []() { return std::array<int, 4>{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) { (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 take_a_span = [](span<const int> s) { (void)s; };
// try to take a temporary std::array
take_a_span(get_an_array());
}
} }
TEST(from_const_std_array_constructor) TEST(from_const_std_array_constructor)
@ -481,6 +487,12 @@ SUITE(span_tests)
#endif #endif
} }
{
auto get_temp_vector = []() -> std::vector<int> { return{}; };
auto use_span = [](span<const int> s) { (void)s; };
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{}; };
@ -489,6 +501,12 @@ SUITE(span_tests)
#endif #endif
} }
{
auto get_temp_string = []() -> std::string { return {}; };
auto use_span = [](span<const char> s) { (void) s; };
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 {}; };
@ -549,7 +567,7 @@ SUITE(span_tests)
#endif #endif
} }
} }
#if 0
TEST(copy_move_and_assignment) TEST(copy_move_and_assignment)
{ {
span<int> s1; span<int> s1;
@ -570,34 +588,7 @@ SUITE(span_tests)
s1 = get_temp_span(); s1 = get_temp_span();
CHECK(s1.length() == 2 && s1.data() == &arr[1]); CHECK(s1.length() == 2 && s1.data() == &arr[1]);
} }
#if 0
template <class Bounds>
void fn(const Bounds&)
{
static_assert(Bounds::static_size == 60, "static bounds is wrong size");
}
TEST(as_span_reshape)
{
int a[3][4][5];
auto av = as_span(a);
fn(av.bounds());
auto av2 = as_span(av, dim<60>());
auto av3 = as_span(av2, dim<3>(), dim<4>(), dim<5>());
auto av4 = as_span(av3, dim<4>(), dim<>(3), dim<5>());
auto av5 = as_span(av4, dim<3>(), dim<4>(), dim<5>());
auto av6 = as_span(av5, dim<12>(), dim<>(5));
fill(av6.begin(), av6.end(), 1);
auto av7 = as_bytes(av6);
auto av8 = as_span<int>(av7);
CHECK(av8.size() == av6.size());
for (auto i = 0; i < av8.size(); i++) {
CHECK(av8[i] == 1);
}
}
TEST(first) TEST(first)
{ {