This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [patch] avoid the use of default constructors in stl_algo.h
- From: Mws <mws at twisted-brains dot org>
- To: libstdc++ at gcc dot gnu dot org
- Cc: Gunter Winkler <guwi17 at gmx dot de>
- Date: Wed, 14 Feb 2007 19:17:15 +0100
- Subject: Re: [patch] avoid the use of default constructors in stl_algo.h
- References: <200702141900.37963.guwi17@gmx.de>
On Wednesday 14 February 2007, Gunter Winkler wrote:
> Hello,
>
> while trying to optimize a custom (random access) iterator I found that
> some implementations of STL algorithms rely on the presence of a
> default constructor [1]. This is IMO unnecessary, because all iterators
> used in algorithms like lower_bound are (modified) copies of the
> initial iterators. Thus only a copy constructor is needed. The attached
> patch replaces declarations like
>
> _ForwardIterator __left;
>
> ...
>
> __left = __first;
>
> by
>
> _ForwardIterator __left = __first;
>
>
> [1] The STL documentation from SGI requires each iterator to have a
> valid default constructor. However, I found no rationale why.
> Additionally, I don't know any algorithm that uses an iterator which is
> not related to the arguments of the function.
>
> The patch has no influence on the results of make check-c++ (SVN head).
>
> What do you think?
>
> mfg
> Gunter
>
hi gunter,
there is - from my point of view - point.
if you construct the forward_iterator like it is implemented it is contructed _once_ during
the whole operation.
the only thing done afterwards is using the assignment operator to adjust its content.
in your case the object is constructed through the copyconstructor
during each cylce of the while loop and destructed afterwards.
so the first implementation you found in the stl sources is more optimised than yours.
also it could be, that a _good_ optimization logic within the compiler would detect this scenario
and reformat your code to the one given.
hope this helps.
please correct me if i understood something wrong.
best regards
marcel