Implement make_span

This commit is contained in:
Glen Fernandes
2023-01-28 12:23:58 -05:00
parent 7736b0b8ce
commit edc0d935c0
5 changed files with 242 additions and 0 deletions

View File

@ -58,6 +58,7 @@ criteria for inclusion is that the utility component be:
[include is_same.qbk]
[include launder.qbk]
[include lightweight_test.qbk]
[include make_span.qbk]
[include max_align.qbk]
[include memory_resource.qbk]
[include no_exceptions_support.qbk]

81
doc/make_span.qbk Normal file
View File

@ -0,0 +1,81 @@
[/
Copyright 2023 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
]
[section:make_span make_span]
[simplesect Authors]
* Glen Fernandes
[endsimplesect]
[section Overview]
The header <boost/core/make_span.hpp> provides function templates `make_span`
to conveniently create `span` objects. They are useful before C++17 where Class
Template Argument Deduction (CTAD) is not available.
[endsect]
[section Reference]
```
namespace boost {
template<class I>
constexpr span<I>
make_span(I* d, std::size_t n) noexcept;
template<class I>
constexpr span<I>
make_span(I* b, I* e) noexcept;
template<class T, std::size_t N>
constexpr span<T, N>
make_span(T(&a)[N]) noexcept;
template<class T, std::size_t N>
constexpr span<T, N>
make_span(std::array<T, N>& a) noexcept;
template<class T, std::size_t N>
constexpr span<const T, N>
make_span(const std::array<T, N>& a) noexcept;
template<class R>
span<std::remove_pointer_t<decltype(std::declval<R&>().data())> >
make_span(R&& r);
} /* boost */
```
[section Functions]
[variablelist
[[`template<class I> span<I> make_span(I* f, std::size_t c);`]
[Returns `span<I>(f, c)`.]]
[[`template<class I> span<I> make_span(I* f, I* l);`]
[Returns `span<I>(f, l)`.]]
[[`template<class T, std::size_t N> span<T, N> make_span(T(&a)[N]);`]
[Returns `span<T, N>(a)`.]]
[[`template<class T, std::size_t N> span<T, N>
make_span(std::array<T, N>& a);`]
[Returns `span<T, N>(a)`.]]
[[`template<class T, std::size_t N> span<const T, N>
make_span(const std::array<T, N>& a);`]
[Returns `span<const T, N>(a)`.]]
[[`template<class R>
span<std::remove_pointer_t<decltype(std::declval<R&>().data())> >
make_span(R&& r);`]
[Returns `span<>(std::forward<R>(r))`.]]]
[endsect]
[endsect]
[endsect]

View File

@ -0,0 +1,59 @@
/*
Copyright 2023 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_CORE_MAKE_SPAN_HPP
#define BOOST_CORE_MAKE_SPAN_HPP
#include <boost/core/span.hpp>
namespace boost {
template<class I>
inline constexpr span<I>
make_span(I* f, std::size_t c) noexcept
{
return span<I>(f, c);
}
template<class I>
inline constexpr span<I>
make_span(I* f, I* l) noexcept
{
return span<I>(f, l);
}
template<class T, std::size_t N>
inline constexpr span<T, N>
make_span(T(&a)[N]) noexcept
{
return span<T, N>(a);
}
template<class T, std::size_t N>
inline constexpr span<T, N>
make_span(std::array<T, N>& a) noexcept
{
return span<T, N>(a);
}
template<class T, std::size_t N>
inline constexpr span<const T, N>
make_span(const std::array<T, N>& a) noexcept
{
return span<const T, N>(a);
}
template<class R>
inline span<typename detail::span_data<R>::type>
make_span(R&& r)
{
return span<typename detail::span_data<R>::type>(std::forward<R>(r));
}
} /* boost */
#endif

View File

@ -343,6 +343,7 @@ run span_deduction_guide_test.cpp ;
run as_bytes_test.cpp ;
run as_writable_bytes_test.cpp ;
compile span_boost_begin_test.cpp ;
run make_span_test.cpp ;
run splitmix64_test.cpp
: : : $(pedantic-errors) ;

100
test/make_span_test.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
Copyright 2023 Glen Joseph Fernandes
(glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
(http://www.boost.org/LICENSE_1_0.txt)
*/
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_DECLTYPE)
#include <boost/core/make_span.hpp>
#include <boost/core/lightweight_test.hpp>
template<class T>
class range {
public:
T* data() {
return &v_[0];
}
std::size_t size() const {
return 4;
}
private:
T v_[4];
};
void test_data_size()
{
int a[4];
boost::span<int> s = boost::make_span(&a[0], 4);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_first_last()
{
int a[4];
boost::span<int> s = boost::make_span(&a[0], &a[4]);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_array()
{
int a[4];
boost::span<int, 4> s = boost::make_span(a);
BOOST_TEST_EQ(s.data(), &a[0]);
BOOST_TEST_EQ(s.size(), 4);
}
void test_std_array()
{
std::array<int, 4> a;
boost::span<int, 4> s = boost::make_span(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_const_std_array()
{
const std::array<int, 4> a = std::array<int, 4>();
boost::span<const int> s = boost::make_span(a);
BOOST_TEST_EQ(s.data(), a.data());
BOOST_TEST_EQ(s.size(), a.size());
}
void test_range()
{
range<int> c;
boost::span<int> s = boost::make_span(c);
BOOST_TEST_EQ(s.data(), c.data());
BOOST_TEST_EQ(s.size(), c.size());
}
void test_initializer_list()
{
std::initializer_list<int> l{1, 2};
boost::span<const int> s = boost::make_span(l);
BOOST_TEST_EQ(s.data(), l.begin());
BOOST_TEST_EQ(s.size(), l.size());
}
int main()
{
test_data_size();
test_first_last();
test_array();
test_std_array();
test_const_std_array();
test_range();
test_initializer_list();
return boost::report_errors();
}
#else
int main()
{
return 0;
}
#endif