Boost C++ Libraries: Ticket #4427: for_each does not take a non-const Function object https://svn.boost.org/trac10/ticket/4427 <p> 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. </p> <p> I was trying to use fusion::foreach for this: </p> <pre class="wiki">FusionType fvar; EntryReader er(stream); // This should call er for each entry in fvar for_each(fvar, er); </pre><p> The problem is that for_each only accepts const references to Function objects, so EntryReader::operator()(T&amp; out) cannot read from the stream. Is it possible to make the Function reference in for_each non-const? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4427 Trac 1.4.3 anonymous Fri, 13 Aug 2010 16:01:47 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4427#comment:1 https://svn.boost.org/trac10/ticket/4427#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> not a bug. to modify data in F, use a function object that holds a reference to mutable data. </p> Ticket anonymous Wed, 13 Jun 2012 12:45:27 GMT <link>https://svn.boost.org/trac10/ticket/4427#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4427#comment:2</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4427#comment:1" title="Comment 1">anonymous</a>: </p> <blockquote class="citation"> <p> not a bug. to modify data in F, use a function object that holds a reference to mutable data. </p> </blockquote> <p> Really? How is this solution better than accepting a non-const version of the operator? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 13 Jun 2012 12:48:43 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/4427#comment:3 https://svn.boost.org/trac10/ticket/4427#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">invalid</span> </li> </ul> Ticket Joel de Guzman Wed, 13 Jun 2012 23:32:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/4427#comment:4 https://svn.boost.org/trac10/ticket/4427#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> FYI, the newer version adds the non-const overload. Please check again. Yep, it was added due to popular demand. </p> Ticket Joel de Guzman Wed, 13 Jun 2012 23:38:24 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/4427#comment:5 https://svn.boost.org/trac10/ticket/4427#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> </ul> <p> 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. </p> Ticket Joel de Guzman Wed, 13 Jun 2012 23:56:40 GMT <link>https://svn.boost.org/trac10/ticket/4427#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4427#comment:6</guid> <description> <p> 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). </p> <p> So, indeed it still stands as -not-a-bug-. See the documentation: <a class="ext-link" href="http://tinyurl.com/7uxyovh"><span class="icon">​</span>http://tinyurl.com/7uxyovh</a> 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. <a class="ext-link" href="http://en.cppreference.com/w/cpp/algorithm/for_each"><span class="icon">​</span>http://en.cppreference.com/w/cpp/algorithm/for_each</a>; notice <a class="missing wiki">UnaryFunction</a> f) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 24 May 2013 18:03:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4427#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4427#comment:7</guid> <description> <p> The documentation does indicate a function object "F f", but the implementation (in 1.53) takes "F const&amp;". So if no copy of the function object is made (and I can't see why it should), it seems: </p> <ol><li>The documentation should be corrected. </li><li>Given that no copy is necessary, why not accept a non-const "F&amp; f" argument? </li></ol> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Mon, 04 Nov 2013 11:13:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4427#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4427#comment:8</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4427#comment:6" title="Comment 6">djowel</a>: </p> <blockquote class="citation"> <p> F can be passed around by value and any changes there will not update the actual F you passed in. </p> </blockquote> <p> F can be wrapped in std::ref and any changes there will update the actual F. </p> <blockquote class="citation"> <p> Even STL algorithms are designed that way, so I am not sure why you are surprised (e.g. <a class="ext-link" href="http://en.cppreference.com/w/cpp/algorithm/for_each"><span class="icon">​</span>http://en.cppreference.com/w/cpp/algorithm/for_each</a>; notice <a class="missing wiki">UnaryFunction</a> f) </p> </blockquote> <p> Notice, that f is passed by value, but not const reference. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 20 Jun 2018 12:56:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4427#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4427#comment:9</guid> <description> <p> 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... </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Joel de Guzman</dc:creator> <pubDate>Wed, 20 Jun 2018 21:40:51 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/4427#comment:10 https://svn.boost.org/trac10/ticket/4427#comment:10 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Joel de Guzman</span> to <span class="trac-author">Kohei Takahashi</span> </li> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">new</span> </li> </ul> Ticket Kohei Takahashi Tue, 03 Jul 2018 03:41:53 GMT status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/4427#comment:11 https://svn.boost.org/trac10/ticket/4427#comment:11 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.44.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> <p> <a class="ext-link" href="https://github.com/boostorg/fusion/pull/183"><span class="icon">​</span>https://github.com/boostorg/fusion/pull/183</a> </p> Ticket Kohei Takahashi Wed, 04 Jul 2018 01:46:08 GMT milestone changed https://svn.boost.org/trac10/ticket/4427#comment:12 https://svn.boost.org/trac10/ticket/4427#comment:12 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.68.0</span> </li> </ul> Ticket