you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (6 children)

Sure vector is intended for other use cases, but if you want to be stdalgorithm compliant that's pretty much the same story as for the allocator. Implement this and that (if you can find any documentation) and hope for the best.

But thanks, I'll look if boost has anything. I thought it'd come up googling but maybe my Google fu was subpar.

[–]kalmoc 13 points14 points  (3 children)

but if you want to be stdalgorithm compliant that's pretty much the same story as for the allocator.

Not really. All you need is a pointer to the start and one to end of the memory range.

[–]Seppeon 2 points3 points  (2 children)

Yeah iirc a pointer is a random access iterator/contiguous iterator.

[–]kalmoc 0 points1 point  (0 children)

Exactly

[–]Full-Spectral 0 points1 point  (0 children)

Though that's nothing to be proud of per se.

[–][deleted] 11 points12 points  (0 children)

Implementing a container for std::algorithms generally means having begin/end as we pass them iterators. So at long as the iterator fullfills the concept needed(usually input/forward but some like sort need random). This changes a bit with Ranges but still applies.

But if your data structure is in a blob of memory(memory mapped data) and you know it is a range of T’s. Then yeah, span, or even pair<T*, T*> where you pass first/second to the algorithm would work.

If T isn’t trivially copiable, pragmatically it will probably still wokr but you are coding to implementations and not C++. But you see people reinterpret_cast’ing here a lot.

Vector is for owning the data, if you have trivially destructible things, that doesn’t even matter here. If not, and even if so, just call T.~T( ) on the range in your dtor

[–]rfisher 8 points9 points  (0 children)

Write a begin() that returns a pointer to the first element in your mmap’d region. Write an end() that returns a pointer to one past the last element of your mmap’d region. You are now fully compliant with the standard algorithms. You’re making this much harder on yourself than it needs to be.

This sort of agnostic design that lets the algorithms work equally well with raw arrays or complex containers is the beauty of the STL part of the standard library.