Opened 12 years ago

Closed 4 years ago

Last modified 4 years 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 by anonymous, 12 years ago

Resolution: invalid
Status: newclosed

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

in reply to:  1 comment:2 by anonymous, 10 years ago

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 by anonymous, 10 years ago

Resolution: invalid
Status: closedreopened

comment:4 by Joel de Guzman, 10 years ago

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 by Joel de Guzman, 10 years ago

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 by Joel de Guzman, 10 years ago

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 by anonymous, 9 years ago

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?

in reply to:  6 comment:8 by anonymous, 9 years ago

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 by anonymous, 4 years ago

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 by Joel de Guzman, 4 years ago

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

comment:11 by Kohei Takahashi, 4 years ago

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

comment:12 by Kohei Takahashi, 4 years ago

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