Boost logo

Boost :

From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2007-10-23 11:56:26


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk