I am receiving managed data in a low level { void * data; uint stride, count; } format. I can read, write and swap data items, but not to add or remove / resize / reallocate. There's enough information to iterate and random access, but no type info for dereferencing. All opaque items can be mem copied and moved trivially.
Is it possible in modern C++ to facilitate std compatibility for such a data format, omitting the dereferencing interface, and delegating the evaluation to an external context? Something like:
std::sort(OpaqueFirst, OpaqueLast, [](Opaque a, Opaque b){ return handle(a, b); });
The basic idea is to create a type that refers to one of your subarrays and define
swapto actually swap their contents:Then we merely have to create a lazy range of such objects, which is easy with
std::views::transformwrapped aroundstd::views::iota, and write the trivial comparator from the question:Unfortunately,
sortdemands to be able to move range elements into temporary variables, not just swap them. That requires a rather more complicated wrapper type that can actually own the data temporarily, which in turn requires dynamic allocation sincearray::strideisn't a constant:Retaining our custom
swapavoids using such allocations for every element swap, so the performance isn't terrible. The unimplementedoperator=(…) constis necessary to convince the library thatbufferis a proxy type that should support sorting via prvalues.A complete example with the full
buffertype exists on Compiler Explorer.