[Development] RFC: Containers member functions for algorithm

André Pönitz apoenitz at t-online.de
Fri Mar 24 22:33:59 CET 2017

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 ?


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.


       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) ... )


   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.

> 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).


More information about the Development mailing list