C++20 added the contiguous_iterator trait, which I can (?) use on a container's iterators to determine whether it has contiguous storage.
But - what if I can only rely on C++11? What's the closest I can get to determining whether a type (let's say it's guaranteed to be a container) has contiguous storage? I realize the answer may not be entirely reliable, or may not cover all cases, but I want to use some "best-effort" kind of function for this.
Make it a trait on the container or range.
Arrays and std arrays are contiguous.
Vectors are contiguous as are std strings.
String views and std spans are also contiguous.
If you allow for easy extension, simply list all of the contiguous containers.
Or, if you want to be lazy and are ok with a heuristic, arrays, plus objects with
T* data()andstd::size_t size() constwhere*x.begin()isT&.But really the types I listed, plus at most a half-dozen custom types in your code base, are going to be it. List them explicitly. It won't grow without bounds because you'll eventually upgrade to C++20.