I believe that this is all defined behavior that will continue to work on any implementation. If you look at the documentation for valarray, it looks like it should be legal so that all other things about ::std::valarray saved. Bare pointers to elements must remain valid until a resize member function is called or valarray is destroyed.
The only real question is whether valarray is valarray in order to hold its elements contiguously or not. And I found the answer to this question in a post . I will stand it here:
Yes, valarray also uses adjacent storage locations. Specific wording from the standard ($ 26.3.2.3 / 3): the expression & a [i + j] == & a [i] + j evaluates to true for all size_t i and size_t j that i + j is less than the length of the variable array a.
Of course, fragments still cannot be used directly with standard algorithms, although creating a slice iterator should not be too complicated. It would be fairly easy to do bidirectional, but much harder (a lot of the complicated math you need to get exactly) to create a random access iterator.
The difference between the two pointers becomes (as someone else said) ::std::ptrdiff_t . It will be a different type on different platforms. I use gcc under 64-bit Fedora 14, and the type is long for me. There is no overhead in this type conversion. This is not even a conversion. The compiler simply does the subtraction, as if the two pointers were prime old numbers, and the result was a plain old number of some type. Using ::std::ptrdiff_t for a type is to make sure that the type of the number used is large enough to hold the difference between any two pointers in the system.
source share