I have an (uncommented…) source file which I’m trying to understand.
static const Map *gCurMap;
static std::vector<Map> mapVec;
then
auto e = mapVec.end();
auto i = mapVec.begin();
while(i!=e) {
// ...
const Map *map = gCurMap = &(*(i++));
// ...
}
I don’t understand what &(*(i++)) does. It does not compile when just using i++, but to me it looks the same, because I’m “incrementing” i, then I’m requesting the value at the given address and then I’m requesting the address of this value?!
Not at all.
&*xis the same asoperator&(operator*(x)), which can be anything you want.It is only true for pointer types like
T * pthat&*pis the same asp. But C++ has user-defined types and overloadable operators.The dereference operator (
*) is typically overloaded for iterators to return a reference to the container element. The effect of the ampersand operator (&) on the container element is up to the class author; if you want to take the address unconditionally, you should usestd::addressof(*i++)(from your favourite header<memory>).