upd: [imp] span

This commit is contained in:
mutouyun 2023-05-27 16:18:18 +08:00
parent 07c13f1b3a
commit 3c1fec6058
3 changed files with 28 additions and 1 deletions

View File

@ -43,7 +43,7 @@ private:
data(U &&model) noexcept
: header_(std::forward<U>(model)) {
auto elements = this->elements();
decltype(elements)::size_type i = 0;
typename decltype(elements)::size_type i = 0;
LIBIMP_TRY {
for (; i < elements.size(); ++i) {
(void)::LIBIMP::construct<element<value_type>>(&elements[i]);

View File

@ -19,6 +19,8 @@
#include "libimp/def.h"
#include "libimp/detect_plat.h"
#include "libimp/countof.h"
#include "libimp/dataof.h"
#if defined(LIBIMP_CPP_20) && defined(__cpp_lib_span)
#include <span>
@ -56,6 +58,10 @@ using is_sized_sentinel_for =
typename std::enable_if<std::is_convertible<decltype(std::declval<S>() - std::declval<I>()),
std::ptrdiff_t>::value>::type;
template <typename T>
using is_continuous_container =
decltype(dataof(std::declval<T>()), countof(std::declval<T>()));
/// \brief Obtain the address represented by p
/// without forming a reference to the object pointed to by p.
/// \see https://en.cppreference.com/w/cpp/memory/to_address
@ -119,6 +125,15 @@ public:
: ptr_ (detail::to_address(first))
, extent_(static_cast<size_type>(last - first)) {}
template <typename U,
typename = detail::is_continuous_container<U>,
typename = detail::is_array_convertible<std::remove_pointer_t<
decltype(dataof(std::declval<U>()))>, element_type>>
constexpr span(U &&u) noexcept(noexcept(dataof (std::forward<U>(u)),
countof(std::forward<U>(u))))
: ptr_ (dataof (std::forward<U>(u)))
, extent_(countof(std::forward<U>(u))) {}
template <typename U, std::size_t E,
typename = detail::is_array_convertible<U, element_type>>
constexpr span(U (&arr)[E]) noexcept

View File

@ -60,4 +60,16 @@ TEST(span, span) {
TEST(span, fmt) {
EXPECT_EQ(imp::fmt(imp::span<int>{}), "");
EXPECT_EQ(imp::fmt(imp::make_span({1, 3, 2, 4, 5, 6, 7})), "1 3 2 4 5 6 7");
}
TEST(span, construct) {
struct test_imp_span_construct {
/* data */
int size() const noexcept { return sizeof(this); }
auto Data() const noexcept { return this; }
} d1;
imp::span<test_imp_span_construct const> sp {d1};
// imp::span<int const> spp {d1};
EXPECT_EQ(sp.size(), d1.size());
EXPECT_EQ(sp.data(), d1.Data());
}