[v3] Remove algos code duplication

Christopher Jefferson chris@bubblescope.net
Mon Mar 26 19:57:00 GMT 2012


A slight extension on my earlier mail.

Your implementation would break this code:

#include <algorithm>

struct S
{
  bool operator==(const S& s)
  { return true; }
};

int main(void)
{
  S s[4];
  std::find_end(s, s+4,s,s+4);
}

(Notice the non-const operator==). Now, it is clear that this isn't standards-compatable code, but it is still broken.

Your technique also is not going to scale to some more commonly used code, in some of the more complex algorithms, such as lexicographic_compare, where you have two arrays, which could have different value_types, and have to do < comparisons both between the first and second array, and the second and first array. In that case you need a class so you can have two (actually four) overloads.

I think for this kind of cleanup in general, we are in a much better state than we were 5 or so years ago, as libc++ (clang's standard library) is using a very similar technique (with a class with several overloads, not a lambda function), so code is going to have to be fixed for that anyway. Also doing the switch under the C++11 flag is tempting to, as quite a bit of "bad" code is going to get broken by that anyway.

So in conclusion, I think this is a good idea, and might be the right time to do it. I'm just not clear on the advantage of lambdas over a class.

Chris

On 25 Mar 2012, at 20:28, François Dumont wrote:

> Hi
> 
>    Here is a patch proposal to remove the duplication of the implementation details of many algos thanks to usage of lambdas in C++ 11 mode. This is what has been experimented in libstdcxx_so_7 branch but abandonned, lambdas are the real answer to this problem.
> 
> 2012-03-22  François Dumont <fdumont@gcc.gnu.org>
> 
>        * include/bits/stl_algo.h: Hide in C++ 11 mode implementation
>        details made useless thanks to usage of lambdas.
>        (__find_if_not): Remove.
>        * include/bits/stl_heap.h: Likewise.
> 
> Tested undex linux x86_64 with
> 
> make check
> 
> and
> 
> make CXXFLAGS=-std=gnu++11 check
> 
> There are some errors building the testsuite entirely in C++11 mode but none coming from this patch.
> 
> Are you interested ?
> 
> François
> <lambda_algos.patch>



More information about the Libstdc++ mailing list