Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An rvalue boost::span doesn't work for algorithms that expect mutable ranges #149

Open
jsg72 opened this issue Apr 10, 2024 · 0 comments
Open

Comments

@jsg72
Copy link

jsg72 commented Apr 10, 2024

An rvalue span does not work for algorithms that modify elements. In the following example, using an lvalue span works, but using an rvalue span fails to compile.

#include <vector>
#include <boost/core/span.hpp>
#include <boost/range/algorithm/fill.hpp>

template <typename T>
boost::span<T> to_span( std::vector<T>& v ) {
        return v;
}

int main() {

   std::vector<unsigned char> vec{ 1, 2, 3, 4, 5 };
   boost::span<unsigned char> sp{ vec };

   boost::fill( sp, 0 ); // works
   boost::fill( to_span(vec), 0); // fails

   return 0;
}

It looks to me that the issue is that boost::begin and boost::end have overloads for lvalue ranges and const lvalue ranges but not rvalue ranges, so if you pass an rvalue range to an algorithm, it gets treated as a const lvalue range and has const iterators. That seems to be the right thing to do for containers, but not so much for proxy types like span.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant