Date: Sat, 24 May 2025 12:43:01 +0100
The Standard guarantees that the following containers' elements are
stored in contiguous memory locations:
basic_string
vector
array
inplace_vector
Therefore if you get the first element's address with:
MyType *p = &mycontainer.front();
Then you can simply add to it to get the elements that follow:
MyType *p5 = p + 5;
If you want to turn a valid iterator into a pointer, you can just do:
auto it = mycontainer.begin();
MyType *p = &*it;
This will work fine on most computers and compilers, even if you
dereference the 'end' iterator. But the Microsoft compiler won't allow
it, nor will g++ or clang if you define _GLIBCXX_DEBUG. And strictly
speaking, the Standard dictates that the behaviour is undefined.
I think some implementations of the C++ standard library (e.g.
Dinkumware, libstdc++) provide you with a function/method to turn a
contiguous iterator into a pointer, but to my knowledge there is no
standard way of doing this. I'm not aware of any papers to solve this
problem -- but for all I know, maybe it snuck into C++26 without me
noticing.
Anyway what I'm saying is that the C++ standard library should provide
a function/method to turn a contiguous iterator into a pointer, which
can be used safely with an 'end' pointer. This could be a standalone
function such as:
auto it = mycontainer.end();
MyType *p = std::contiguous_p(it);
or perhaps a member function of the iterator class:
auto it = mycontainer.end();
MyType *p = it.to_pointer();
There's no excuse for the Standard not providing this.
stored in contiguous memory locations:
basic_string
vector
array
inplace_vector
Therefore if you get the first element's address with:
MyType *p = &mycontainer.front();
Then you can simply add to it to get the elements that follow:
MyType *p5 = p + 5;
If you want to turn a valid iterator into a pointer, you can just do:
auto it = mycontainer.begin();
MyType *p = &*it;
This will work fine on most computers and compilers, even if you
dereference the 'end' iterator. But the Microsoft compiler won't allow
it, nor will g++ or clang if you define _GLIBCXX_DEBUG. And strictly
speaking, the Standard dictates that the behaviour is undefined.
I think some implementations of the C++ standard library (e.g.
Dinkumware, libstdc++) provide you with a function/method to turn a
contiguous iterator into a pointer, but to my knowledge there is no
standard way of doing this. I'm not aware of any papers to solve this
problem -- but for all I know, maybe it snuck into C++26 without me
noticing.
Anyway what I'm saying is that the C++ standard library should provide
a function/method to turn a contiguous iterator into a pointer, which
can be used safely with an 'end' pointer. This could be a standalone
function such as:
auto it = mycontainer.end();
MyType *p = std::contiguous_p(it);
or perhaps a member function of the iterator class:
auto it = mycontainer.end();
MyType *p = it.to_pointer();
There's no excuse for the Standard not providing this.
Received on 2025-05-24 11:43:13