I'd like to be able to pass my custom container this std::span constructor:
template< class R >
explicit(extent != std::dynamic_extent)
constexpr span( R&& range );
What do I need to add to my custom class to make it satisfy the requirements to be able to pass it to the std::span constructor that receives a range?
For example, with std::vector we are able to do this:
std::vector<int> v = {1, 2, 3};
auto span = std::span{v};
I've already added these to my custom class:
Type* begin()
{
return m_Data;
}
Type* end()
{
return m_Data + m_Length;
}
const Type* data() const
{
return m_Data;
}
size_t size() const
{
return m_Length;
}
...which in turn allowed me to use range-based loops, and std::data(my_container) as well as std::size(my_container). What am I missing to make it so that I can also pass my container to the std::span constructor? Does it require to implement a more complex iterator?
This is because your
Containerdoes not satisfycontiguous_range, which is defined as:In the
requiresclause, the return type ofranges::data(t)isconst Type*, which is different fromadd_pointer_t<range_reference_t<T>>that isType*.The workaround is adding a non-
constdata()to yourContainerwhich returnType*: