Opened 8 years ago

Closed 4 months ago

Last modified 4 months ago

#4427 closed Bugs (fixed)

for_each does not take a non-const Function object

Reported by: chetant@… Owned by: Kohei Takahashi
Milestone: Boost 1.68.0 Component: fusion
Version: Boost 1.43.0 Severity: Problem
Keywords: for_each Cc:

Description

I'm trying to create a type of deserializer, templated by fusion types. I have a function object that I initialize with an input stream and its templatized operator() takes a record out of that stream and assigns it to a given input type.

I was trying to use fusion::foreach for this:

FusionType fvar;
EntryReader er(stream);
// This should call er for each entry in fvar
for_each(fvar, er);

The problem is that for_each only accepts const references to Function objects, so EntryReader::operator()(T& out) cannot read from the stream. Is it possible to make the Function reference in for_each non-const?

Change History (12)

comment:1 Changed 8 years ago by anonymous

Resolution: invalid
Status: newclosed

not a bug. to modify data in F, use a function object that holds a reference to mutable data.

comment:2 in reply to:  1 Changed 6 years ago by anonymous

Replying to anonymous:

not a bug. to modify data in F, use a function object that holds a reference to mutable data.

Really? How is this solution better than accepting a non-const version of the operator?

comment:3 Changed 6 years ago by anonymous

Resolution: invalid
Status: closedreopened

comment:4 Changed 6 years ago by Joel de Guzman

Resolution: fixed
Status: reopenedclosed

FYI, the newer version adds the non-const overload. Please check again. Yep, it was added due to popular demand.

comment:5 Changed 6 years ago by Joel de Guzman

Resolution: fixed
Status: closedreopened

Reopening. It seems that the non-const overload has been there for some time now, so my comment above may be bogus. Please check it and provide a minimal test case if necessary (if it's not yet fixed). Pardon the potential confusion.

comment:6 Changed 6 years ago by Joel de Guzman

And indeed my comments were all bogus. Please ignore. I realize now that this is about adding a non-const overload for function object, not the sequence (which has been there for a long time now).

So, indeed it still stands as -not-a-bug-. See the documentation: http://tinyurl.com/7uxyovh It is clear there that you can't expect F to be mutable. F can be passed around by value and any changes there will not update the actual F you passed in. Even STL algorithms are designed that way, so I am not sure why you are surprised (e.g. http://en.cppreference.com/w/cpp/algorithm/for_each; notice UnaryFunction? f)

comment:7 Changed 5 years ago by anonymous

The documentation does indicate a function object "F f", but the implementation (in 1.53) takes "F const&". So if no copy of the function object is made (and I can't see why it should), it seems:

  1. The documentation should be corrected.
  2. Given that no copy is necessary, why not accept a non-const "F& f" argument?

comment:8 in reply to:  6 Changed 5 years ago by anonymous

Replying to djowel:

F can be passed around by value and any changes there will not update the actual F you passed in.

F can be wrapped in std::ref and any changes there will update the actual F.

Even STL algorithms are designed that way, so I am not sure why you are surprised (e.g. http://en.cppreference.com/w/cpp/algorithm/for_each; notice UnaryFunction? f)

Notice, that f is passed by value, but not const reference.

comment:9 Changed 5 months ago by anonymous

I was wondering whether there's been any news on this? Even with the 1.67 boost version, for_each cannot take a non-cost F...

comment:10 Changed 5 months ago by Joel de Guzman

Owner: changed from Joel de Guzman to Kohei Takahashi
Status: reopenednew

comment:11 Changed 4 months ago by Kohei Takahashi

Milestone: Boost 1.44.0To Be Determined
Resolution: fixed
Status: newclosed

comment:12 Changed 4 months ago by Kohei Takahashi

Milestone: To Be DeterminedBoost 1.68.0
Note: See TracTickets for help on using tickets.