[iterator] could non-small predicates/functors be stored as boost::optional<T> to avoid double construction?

Hi, The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed? best regards -Thorsten

Hi,
The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed?
best regards
-Thorsten
I guess that would be restricted to forward iterators. Arno -- Dr. Arno Schoedl · aschoedl@think-cell.com Technical Director think-cell Software GmbH · Invalidenstr. 34 · 10115 Berlin, Germany http://www.think-cell.com · phone +49-30-666473-10 · toll-free (US) +1-800-891-8091 Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Charlottenburg, HRB 85229

Arno Schödl skrev:
Hi,
The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed?
best regards
-Thorsten
I guess that would be restricted to forward iterators.
This has nothing to do with the wrapped iterator's type. A transform iterator currently stores a functor by value in addition to the wrapped iterator. -Thorsten

on Thu Sep 04 2008, Thorsten Ottosen <thorsten.ottosen-AT-dezide.com> wrote:
Arno Schödl skrev:
Hi,
The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed?
best regards
-Thorsten
I guess that would be restricted to forward iterators.
This has nothing to do with the wrapped iterator's type. A transform iterator currently stores a functor by value in addition to the wrapped iterator.
Have you forgotten *--end ? -- Dave Abrahams BoostPro Computing http://www.boostpro.com

David Abrahams skrev:
on Thu Sep 04 2008, Thorsten Ottosen <thorsten.ottosen-AT-dezide.com> wrote:
Arno Schödl skrev:
Hi, The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed? best regards -Thorsten I guess that would be restricted to forward iterators. This has nothing to do with the wrapped iterator's type. A transform iterator currently stores a functor by value in addition to the wrapped iterator.
Have you forgotten
*--end
Yes. Sorry for the noise. -Thorsten

On Thu, Sep 4, 2008 at 8:18 PM, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
David Abrahams skrev:
on Thu Sep 04 2008, Thorsten Ottosen <thorsten.ottosen-AT-dezide.com> wrote:
Arno Schödl skrev:
Hi, The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed? best regards -Thorsten
I guess that would be restricted to forward iterators.
This has nothing to do with the wrapped iterator's type. A transform iterator currently stores a functor by value in addition to the wrapped iterator.
Have you forgotten
*--end
Yes. Sorry for the noise.
There is still some value in storing functors in optional<T>: boost.lambda expressions are not asignable, thus rendering any iterator that contains a lambda does no longer meet the requirements of Assignable of which TrivialIterator is a refinement of. It is easy to add a wrapper around optional<T> that makes it Assignable as long as T is copy constructible (which lambdas are), so it can be used as a quick workaround for this problem. Many algorithms do not actually require assignability, so I do not think it is worth to explicitly workaround boost lambda in every iterator. Some external mean is better. Egg for example had a 'regular' function which wrapped a lambda in an optional on the fly. -- gpd

on Thu Sep 04 2008, "Giovanni Piero Deretta" <gpderetta-AT-gmail.com> wrote:
On Thu, Sep 4, 2008 at 8:18 PM, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
David Abrahams skrev:
on Thu Sep 04 2008, Thorsten Ottosen <thorsten.ottosen-AT-dezide.com> wrote:
Arno Schödl skrev:
Hi, The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed? best regards -Thorsten
I guess that would be restricted to forward iterators.
This has nothing to do with the wrapped iterator's type. A transform iterator currently stores a functor by value in addition to the wrapped iterator.
Have you forgotten
*--end
Yes. Sorry for the noise.
There is still some value in storing functors in optional<T>: boost.lambda expressions are not asignable, thus rendering any iterator that contains a lambda does no longer meet the requirements of Assignable of which TrivialIterator is a refinement of.
It is easy to add a wrapper around optional<T> that makes it Assignable as long as T is copy constructible (which lambdas are), so it can be used as a quick workaround for this problem.
But for that you wouldn't need the bool in optional. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

AMDG David Abrahams wrote:
There is still some value in storing functors in optional<T>: boost.lambda expressions are not asignable, thus rendering any iterator that contains a lambda does no longer meet the requirements of Assignable of which TrivialIterator is a refinement of.
It is easy to add a wrapper around optional<T> that makes it Assignable as long as T is copy constructible (which lambdas are), so it can be used as a quick workaround for this problem.
But for that you wouldn't need the bool in optional.
I think that you do need it, actually. I assume that assignment would be implemented with destroy + reconstruct? In this case, you need to deal with the possibility of the copy constructor throwing. In Christ, Steven Watanabe

on Thu Sep 04 2008, Thorsten Ottosen <thorsten.ottosen-AT-dezide.com> wrote:
Hi,
The headline pretty much says it all. Take transform iterator. When the predicate is not trivial, we also have a redundant construction of the object in the end iterator. Maybe that could be removed?
It would increase the overall size because optional<T> needs to store a bool as well as a T. It would work for any kind of iterator traversal because the function object would need to be constructed on demand (you have to do that even for forward iterators because you can't know that the end iterator of a range isn't known by the user to be somewhere in a larger range -- the user can always dereference it in that case), but that extra check upon dereference would also increase cost. I am not really convinced it's worthwhile. -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (5)
-
Arno Schödl
-
David Abrahams
-
Giovanni Piero Deretta
-
Steven Watanabe
-
Thorsten Ottosen