Title
Is for_each overconstrained?
Status
c++11
Section
[alg.foreach]
Submitter
Alisdair Meredith

Created on 2009-04-29.00:00:00 last changed 154 months ago

Messages

Date: 2010-10-21.18:28:33

Proposed resolution:

  • Add a new Requires clause just after the prototype declaration ([alg.foreach]):

    Requires: Function shall meet the requirements of MoveConstructible ([moveconstructible]). Function need not meet the requirements of CopyConstructible ([copyconstructible]).

  • Change [alg.foreach]/2 as indicated:

    Returns: std::move(f).

Date: 2009-11-22.00:00:00

[ 2009-11-22 Moved to Tentatively Ready after 5 positive votes on c++std-lib. ]

Date: 2009-11-20.00:00:00

[ 2009-11-20 Howard restores "not CopyConstructible" to the spec. ]

Date: 2009-10-27.00:00:00

[ 2009-10-27 Daniel adds: ]

I see that during the Santa Cruz meeting the originally proposed addition

, CopyConstructible is not required.

was removed. I don't think that this removal was a good idea. The combination of [algorithms.general] p.9

[Note: Unless otherwise specified, algorithms that take function objects as arguments are permitted to copy those function objects freely.[..]

with the fact that CopyConstructible is a refinement MoveConstructible makes it necessary that such an explicit statement is given. Even the existence of the usage of std::move in the Returns clause doesn't help much, because this would still be well-formed for a CopyConstructible without move constructor. Let me add that the originally proposed addition reflects current practice in the standard, e.g. [alg.unique] p.5 usages a similar terminology.

For similar wording need in case for auto_ptr see 973.

Howard: Moved from Tentatively Ready to Open.
Date: 2010-10-21.18:28:33

[ 2009-10 post-Santa Cruz: ]

Move to Tentatively Ready, using Daniel's wording without the portion saying "CopyConstructible is not required".

Date: 2009-10-15.00:00:00

[ 2009-10-15 Daniel proposes: ]

  • Add a new Requires clause just after the prototype declaration ([alg.foreach]):

    Requires: Function shall be MoveConstructible ([moveconstructible]), CopyConstructible is not required.

  • Change [alg.foreach]/2 as indicated:

    Returns: std::move(f).

Date: 2009-10-14.00:00:00

[ 2009-10-14 Daniel de-conceptified the proposed resolution. ]

The note in [algorithms.general]/9 already says the right thing:

Unless otherwise specified, algorithms that take function objects as arguments are permitted to copy those function objects freely.

So we only need to ensure that the wording for for_each is sufficiently clear, which is the intend of the following rewording.

Date: 2010-10-21.18:28:33

[ Batavia (2009-05): ]

Pete suggests we may want to look at this in a broader context involving other algorithms. We should also consider the implications of parallelism.

Move to Open, and recommend the issue be deferred until after the next Committee Draft is issued.

Date: 2010-10-21.18:28:33

[ See further discussion starting with c++std-lib-23686. ]

Date: 2009-04-29.00:00:00

Quoting working paper for reference ([alg.foreach]):

template<InputIterator Iter, Callable<auto, Iter::reference> Function>
  requires CopyConstructible<Function>
  Function for_each(Iter first, Iter last, Function f);

1 Effects: Applies f to the result of dereferencing every iterator in the range [first,last), starting from first and proceeding to last - 1.

2 Returns: f.

3 Complexity: Applies f exactly last - first times.

P2 implies the passed object f should be invoked at each stage, rather than some copy of f. This is important if the return value is to usefully accumulate changes. So the requirements are an object of type Function can be passed-by-value, invoked multiple times, and then return by value. In this case, MoveConstructible is sufficient. This would open support for move-only functors, which might become important in concurrent code as you can assume there are no other references (copies) of a move-only type and so freely use them concurrently without additional locks.

History
Date User Action Args
2011-08-23 20:07:26adminsetstatus: wp -> c++11
2010-10-21 18:28:33adminsetmessages: + msg777
2010-10-21 18:28:33adminsetmessages: + msg776
2010-10-21 18:28:33adminsetmessages: + msg775
2010-10-21 18:28:33adminsetmessages: + msg774
2010-10-21 18:28:33adminsetmessages: + msg773
2010-10-21 18:28:33adminsetmessages: + msg772
2010-10-21 18:28:33adminsetmessages: + msg771
2010-10-21 18:28:33adminsetmessages: + msg770
2010-10-21 18:28:33adminsetmessages: + msg769
2009-04-29 00:00:00admincreate