[Development] RFC: Containers member functions for algorithm

Eike Ziller Eike.Ziller at qt.io
Mon Mar 27 09:36:15 CEST 2017


> On Mar 24, 2017, at 10:33 PM, André Pönitz <apoenitz at t-online.de> wrote:
> 
> On Fri, Mar 24, 2017 at 04:25:34PM +0000, Corentin wrote:
>> Is std::algo(std::begin(container), std::end(container) ... )  troublesome
>> enough that it warrants a wrapper ?
> 
> Yes.
> 
> 1. It is more to read, and more to verify during reading, e.g. 
>   that bother 'container' are the same.
> 
>   This typically implies that 'container' is just a simple
>   identifier for a local variable (i.e. usuall an extra line for
>   a definition) or a member of the current object (rare).
> 
>   Even seeing 
> 
>       std::algo(std::begin(foo), std::end(foo) ... )
> 
>   *verbatim* with a plain 'foo' does not guarantee that both
>   'foo' refer to the same container, i.e. potentially needs
>   verification when bug hunting.
> 
>   For 
> 
>       std::algo(std::begin(foo()), std::end(foo()) ... )
> 
>   chances are high that it is wrong. Except when it isn't.
>   But for that you need to consult the declaration, and
>   even possibly the implementation of 'foo'.
> 
> 
> 2. It more to type.
> 
>   std::algo(std::begin(container), std::end(container) ... )
> 
>     vs
> 
>   foo::algo(container, ... )
> 
>   Depending on your project's coding style there are additional
>   complications, e.g. that the ~55 chars of the first are 'a lot'
>   in a 80 char-per-line setup.

And begin and end are not the only things that can be gotten rid of.
The back_inserters you need for things like transform and copy_if are noise for the simple/common case,
and I’ve seen a few patches already that forgot the erase after remove_if.

> 
>> Your last example is simplified by the Library Fundamentals TS
>> https://rawgit.com/cplusplus/fundamentals-ts/v2/fundamentals-ts.html#container.erasure
>> erase_if(std::begin(myList), std::end(myList), [&](const auto &elem) {
>> elem.field > someValue; });
> 
> 100 chars, vs 70 chars for a no-begin-end-cluttered
> 
> erase_if(myList, [&](const auto &elem) { elem.field > someValue; });
> 
> and some hope that it going from 'myList' to 'myList()' is really
> just two chars change.
> 
> Not Nice (TM).
> 
> Andre'
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development

-- 
Eike Ziller
Principal Software Engineer

The Qt Company GmbH
Rudower Chaussee 13
D-12489 Berlin
eike.ziller at qt.io
http://qt.io
Geschäftsführer: Mika Pälsi,
Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B



More information about the Development mailing list