diff --git a/include/libtorrent/span.hpp b/include/libtorrent/span.hpp index 583eb30f9..6d635eb37 100644 --- a/include/libtorrent/span.hpp +++ b/include/libtorrent/span.hpp @@ -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 ().data())>::type + , typename = typename std::enable_if::value + && std::is_const::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; } diff --git a/test/test_span.cpp b/test/test_span.cpp index f3d87cfc5..b4433f693 100644 --- a/test/test_span.cpp +++ b/test/test_span.cpp @@ -52,6 +52,18 @@ TORRENT_TEST(span_vector) TEST_CHECK(a.size() == 4); } +void do_span_temp_vector(span a) +{ + std::vector 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{1,2,3,4}); +} + TORRENT_TEST(span_std_array) { std::array v1{{1,2,3,4}};