#4427 closed Bugs (fixed)
for_each does not take a non-const Function object
Reported by: | 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)
follow-up: 2 comment:1 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 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 , 10 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
comment:4 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
FYI, the newer version adds the non-const overload. Please check again. Yep, it was added due to popular demand.
comment:5 by , 10 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
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.
follow-up: 8 comment:6 by , 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 , 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:
- The documentation should be corrected.
- Given that no copy is necessary, why not accept a non-const "F& f" argument?
comment:8 by , 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 , 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 , 4 years ago
Owner: | changed from | to
---|---|
Status: | reopened → new |
comment:11 by , 4 years ago
Milestone: | Boost 1.44.0 → To Be Determined |
---|---|
Resolution: | → fixed |
Status: | new → closed |
comment:12 by , 4 years ago
Milestone: | To Be Determined → Boost 1.68.0 |
---|
not a bug. to modify data in F, use a function object that holds a reference to mutable data.