[interprocess] basic_multiallocation_iterator, operator * and -> question

Hello, in the file mem_algo_common.hpp there is a class basic_multiallocation_iterator (line 50ff). This class contains both definitions for operator* and operator->, where operator* is implemented as: value_type operator*() const { value_type v = (char*)detail::get_pointer(next_alloc_.next_); return v; } and operator->() as: pointer operator->() const { return &operator*(); } The second definition confuses both me and my compiler. Does &operator*() actually mean &(*this)? If yes, wouldn't this mean that operator-> returns the address of a stack allocated result variable, namely the address of the result of operator*? If no, what does this actually mean? Thanks for enlightening me, Markus

Markus Schöpflin escribió:
Hello,
in the file mem_algo_common.hpp there is a class basic_multiallocation_iterator (line 50ff).
This class contains both definitions for operator* and operator->, where operator* is implemented as:
value_type operator*() const { value_type v = (char*)detail::get_pointer(next_alloc_.next_); return v; }
and operator->() as:
pointer operator->() const { return &operator*(); }
The second definition confuses both me and my compiler. Does &operator*() actually mean &(*this)? If yes, wouldn't this mean that operator-> returns the address of a stack allocated result variable, namely the address of the result of operator*? If no, what does this actually mean?
You are right. I'm returning the address of a temporary! I think conceptually the iterator is wrong. I should return char & for operator*() instead of char *. char *should be returned by operator->. However this makes the code uglier: multiallocation_iterator it = ... for(...) //placement new new(&*it++) T(); instead of for(...) //placement new new(*it++) T(); If I want basic_multiallocation_iterator to be a well-formed iterator I need to define operator->() so I think I'll need to change the interface of this class (I doubt anyone is using it). Give me some time to change it and pass the tests again. Regards, Ion

Ion Gaztañaga schrieb:
You are right. I'm returning the address of a temporary! I think conceptually the iterator is wrong. I should return char & for operator*() instead of char *. char *should be returned by operator->.
[...]
If I want basic_multiallocation_iterator to be a well-formed iterator I need to define operator->() so I think I'll need to change the interface of this class (I doubt anyone is using it).
If it is unused, remove it I'd say.
Give me some time to change it and pass the tests again.
In the mean time, could you perhaps change the code in question to &(*this)? This would at least make the compilation error go away. And perhaps insert an assert(false), to mark the code as invalid. Markus

Markus Schöpflin escribió:
In the mean time, could you perhaps change the code in question to &(*this)? This would at least make the compilation error go away. And perhaps insert an assert(false), to mark the code as invalid.
Done. I've also managed_memory_impl error.
Markus
Ion

AMDG Ion Gaztañaga <igaztanaga <at> gmail.com> writes:
You are right. I'm returning the address of a temporary! I think conceptually the iterator is wrong. I should return char & for operator*() instead of char *. char *should be returned by operator->.
However this makes the code uglier:
multiallocation_iterator it = ...
for(...) //placement new new(&*it++) T();
instead of
for(...) //placement new new(*it++) T();
If I want basic_multiallocation_iterator to be a well-formed iterator I need to define operator->() so I think I'll need to change the interface of this class (I doubt anyone is using it). Give me some time to change it and pass the tests again.
It isn't /necessary/. template<class T, class pointer> struct arrow_proxy { arrow_proxy(const T& t) : impl(t) {} pointer operator->() const { return(&impl); } T impl; } arrow_proxy<value_type, pointer> operator->() { return(arrow_proxy<value_type, pointer>(**this)); } In Christ, Steven Watanabe
participants (4)
-
Ion Gaztañaga
-
Markus Schöpflin
-
Markus Schöpflin
-
Steven Watanabe