add span ctor from const containers (#2275)

This commit is contained in:
Steven Siloti 2017-08-28 02:36:51 -07:00 committed by Arvid Norberg
parent d4986f878f
commit cc3f73de96
2 changed files with 21 additions and 0 deletions

View File

@ -90,6 +90,15 @@ namespace aux {
span(Cont& c) // NOLINT
: m_ptr(c.data()), m_len(c.size()) {}
// allow construction from const containers if T is const
// this allows const spans to be constructed from a temporary container
template <typename Cont
, typename U = typename std::remove_reference<decltype(*std::declval<Cont>().data())>::type
, typename = typename std::enable_if<aux::compatible_type<U, T>::value
&& std::is_const<T>::value>::type>
span(Cont const& c) // NOLINT
: m_ptr(c.data()), m_len(c.size()) {}
std::size_t size() const noexcept { return m_len; }
bool empty() const noexcept { return m_len == 0; }
T* data() const noexcept { return m_ptr; }

View File

@ -52,6 +52,18 @@ TORRENT_TEST(span_vector)
TEST_CHECK(a.size() == 4);
}
void do_span_temp_vector(span<char const> a)
{
std::vector<char> v1 = {1,2,3,4};
TEST_CHECK(a == f(v1));
TEST_CHECK(a.size() == 4);
}
TORRENT_TEST(span_temp_vector)
{
do_span_temp_vector(std::vector<char>{1,2,3,4});
}
TORRENT_TEST(span_std_array)
{
std::array<char, 4> v1{{1,2,3,4}};