Boost C++ Libraries: Ticket #4189: Add better support for non-default constructible function objects https://svn.boost.org/trac10/ticket/4189 <p> boost::transform_iterator is required to be default constructible, but not all function objects are. For example, boost::mem_fn_t is not default constructible. To allow transform_iterator to model <a class="missing wiki">DefaultConstructible</a> with any function object type, Peter Dimov suggested using boost::optional. Thanks to Samuel Debionne for identifying and reporting the problem. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4189 Trac 1.4.3 Daniel Walker Tue, 04 May 2010 22:44:27 GMT attachment set https://svn.boost.org/trac10/ticket/4189 https://svn.boost.org/trac10/ticket/4189 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">transform_iterator.patch</span> </li> </ul> <p> use boost::optional to store function objects </p> Ticket Daniel Walker Wed, 05 May 2010 21:00:12 GMT attachment set https://svn.boost.org/trac10/ticket/4189 https://svn.boost.org/trac10/ticket/4189 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">transform_iterator.2.patch</span> </li> </ul> <p> use boost::optional to store function objects with non-trivial constructors as suggested by Jeffrey Lee Hellrung </p> Ticket sdebionne Fri, 22 Jul 2011 12:06:39 GMT <link>https://svn.boost.org/trac10/ticket/4189#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:1</guid> <description> <p> This patch solves the problem mentionned here : </p> <p> <a class="ext-link" href="http://boost.2283326.n4.nabble.com/Bind-Interoperability-of-bind-mem-fn-with-transform-iterator-td2668312.html"><span class="icon">​</span>http://boost.2283326.n4.nabble.com/Bind-Interoperability-of-bind-mem-fn-with-transform-iterator-td2668312.html</a> </p> <p> I have been using it for more than a year. Could you considerer merging it to trunk ? </p> <p> the problem is even more acute with Boost.Range adaptors since something as simple as </p> <pre class="wiki">using namespace boost::range::adaptors; boost::for_each(my_range | transformed(boost::mem_fn(foo)), bar); </pre><p> won't compile. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Fri, 22 Jul 2011 20:00:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4189#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:2</guid> <description> <p> I'm concerned about the mandatory overhead implied by the use of boost::optional for transform iterators that may not need it. What about making a function object wrapper that adds default-constructibility? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 21 Nov 2012 19:44:05 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/4189#comment:3 https://svn.boost.org/trac10/ticket/4189#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Dave Abrahams</span> to <span class="trac-author">jeffrey.hellrung</span> </li> </ul> Ticket Taras Kozlov Tue, 13 Aug 2013 10:38:32 GMT <link>https://svn.boost.org/trac10/ticket/4189#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:4</guid> <description> <p> I<code>ve suffered from the same problem, when I tried to use it with lambdas. If lambda has non-empty capture list, it doesn</code>t provide constructor with zero arguments. </p> <pre class="wiki">typedef boost::any_range&lt;int, boost::random_access_traversal_tag, int, ptrdiff_t&gt; int_range; int_range f(const vector&lt;double&gt;&amp; v) { vector&lt;double&gt; vec; string str = "Hello"; return vec | boost::adaptors::transformed([str](double v) -&gt; int { return (int)v; }); } </pre><p> If boost::optional overhead is inacceptable, we may try to use aligned storage + placement new instead. </p> <p> I also want to notice simple workaround: assigning lambda to std::function will make it work </p> <pre class="wiki">std::function&lt;int(double)&gt; lambda = [str](double v) -&gt; int { return (int)v; }; return vec | transformed(lambda); </pre><p> This is not the perfect solution since it involves runtime overhead especially on large data ranges. </p> </description> <category>Ticket</category> </item> <item> <author>olivier-m.pena@…</author> <pubDate>Wed, 21 Aug 2013 22:54:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4189#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:5</guid> <description> <p> Same problem than Taras here : transform_iterator assigned to an any_range with gcc 4.8.1. Even without closure. </p> <p> If not assigned to an any_range, it's ok. </p> <pre class="wiki">typedef boost::any_range&lt;int, boost::forward_traversal_tag, int, std::ptrdiff_t&gt; int_range; std::vector&lt;int&gt; v; // Fail. int_range range1 = v | boost::adaptors::transformed( [](const int&amp; i) {return i * 2; }); // Ok. int_range range2 = v | boost::adaptors::transformed( std::function&lt;int(const int&amp;)&gt;( [](const int&amp; i) {return i * 2; })); </pre><p> I would like to add that clang 3.3 compiles successfully. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Thu, 22 Aug 2013 04:47:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4189#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:6</guid> <description> <p> As far as <code>transform_iterator</code> is concerned, there's nothing it can do about non-default-constructible function objects in C++03 without also penalizing uses of default-constructible function objects. If there's a C++11 type trait that allows detection of deleted default ctors that would sort of let the iterator library introduce the <code>optional&lt;F&gt;</code> wrapper itself, but it wouldn't help if the default ctor was private. My suggestion here would be to create a generic forwarding function object wrapper around <code>optional&lt;F&gt;</code> and use that as the function object for your <code>transform_iterator</code>. </p> </description> <category>Ticket</category> </item> <item> <author>olivier-m.pena@…</author> <pubDate>Fri, 23 Aug 2013 07:29:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4189#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4189#comment:7</guid> <description> <p> Thank you for the explanations. With C++11, std::function may be that wrapper. But I still can't figure out why clang compiles my example ? </p> </description> <category>Ticket</category> </item> </channel> </rss>