Boost C++ Libraries: Ticket #5408: fold(transform(.... )) gives erroneous result: dangling reference? https://svn.boost.org/trac10/ticket/5408 <p> The code below demonstrates a problem using fusion's transform on a fold view in conjunction with a phoenix functional. It produces the output: </p> <blockquote> <p> Should be the same: 1 =? 4.59177e-41 </p> </blockquote> <p> it should produce </p> <blockquote> <p> Should be the same: 1 =? 1 </p> </blockquote> <p> Thanks, Joe </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;cmath&gt; #include &lt;boost/detail/lightweight_test.hpp&gt; #include &lt;boost/spirit/include/phoenix_core.hpp&gt; #include &lt;boost/spirit/include/phoenix_operator.hpp&gt; #include &lt;boost/spirit/include/phoenix_function.hpp&gt; #include &lt;boost/fusion/container.hpp&gt; #include &lt;boost/fusion/functional.hpp&gt; #include &lt;boost/fusion/include/io.hpp&gt; #include &lt;boost/fusion/include/fold.hpp&gt; #include &lt;boost/fusion/include/transform.hpp&gt; #include &lt;boost/type_traits.hpp&gt; #include &lt;boost/mpl/assert.hpp&gt; using namespace boost::fusion; using namespace boost::phoenix; typedef float OutputType; // A general error function, measuring the difference between two output patterns. struct error_function3 { template &lt;typename Outputs1, typename Outputs2&gt; struct result { typedef OutputType type; }; template &lt;typename Outputs1, typename Outputs2&gt; OutputType operator()(Outputs1&amp; outputs1, Outputs2&amp; outputs2) const { using boost::phoenix::arg_names::_1; using boost::phoenix::arg_names::_2; using boost::fusion::fold; using boost::fusion::transform; float bad_result = fold(transform(outputs1, outputs2, (_1 - _2) * (_1 - _2)), 0, _1 + _2); float good_result = fold(as_vector(transform(outputs1, outputs2, (_1 - _2) * (_1 - _2))), 0, _1 + _2); std::cout &lt;&lt; "Should be the same: " &lt;&lt; good_result &lt;&lt; " =? " &lt;&lt; bad_result &lt;&lt; std::endl; return good_result; } }; int main() { using boost::phoenix::arg_names::_1; using boost::phoenix::arg_names::_2; vector&lt;OutputType, OutputType&gt; o1 = make_vector(1., 2.); vector&lt;OutputType, OutputType&gt; o2 = make_vector(2., 3.); boost::phoenix::function&lt;error_function3&gt; const error; std::cout &lt;&lt; error(_1, _2)(o1, o2) &lt;&lt; std::endl; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5408 Trac 1.4.3 anonymous Fri, 01 Apr 2011 12:10:56 GMT summary changed https://svn.boost.org/trac10/ticket/5408#comment:1 https://svn.boost.org/trac10/ticket/5408#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">transform(fold(.... )) gives erroneous result: dangling reference?</span> → <span class="trac-field-new">fold(transform(.... )) gives erroneous result: dangling reference?</span> </li> </ul> Ticket Joel de Guzman Fri, 01 Apr 2011 23:25:27 GMT cc set https://svn.boost.org/trac10/ticket/5408#comment:2 https://svn.boost.org/trac10/ticket/5408#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">joel@…</span> added </li> </ul> <p> I suggest you use Phoenix-3 instead. It is in the Boost trunk. The problem here is that Phoenix-2 has a old and flaky result-type deduction scheme. Phoenix-3 is better. The changes required are minimal. First use the proper includes: </p> <blockquote> <p> #include &lt;boost/phoenix/core.hpp&gt; #include &lt;boost/phoenix/operator.hpp&gt; #include &lt;boost/phoenix/function.hpp&gt; </p> </blockquote> <p> Then, change the result-of of error_function3: </p> <blockquote> <p> typedef <a class="missing wiki">OutputType</a> result_type; </p> </blockquote> <p> With the changes, I am getting: </p> <blockquote> <p> Should be the same: 2 =? 2 2 </p> </blockquote> Ticket joe@… Tue, 05 Apr 2011 11:13:06 GMT <link>https://svn.boost.org/trac10/ticket/5408#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5408#comment:3</guid> <description> <p> Yes, that's fine! :) Now that I understand the changes to result_of I'm happy with moving to Pheonix 3. </p> </description> <category>Ticket</category> </item> <item> <author>Kohei Takahashi <flast@…></author> <pubDate>Wed, 05 Nov 2014 13:19:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5408#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5408#comment:4</guid> <description> <p> Phoenix V2 is already removed from release tarball. please close. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Joel de Guzman</dc:creator> <pubDate>Wed, 05 Nov 2014 15:05:50 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5408#comment:5 https://svn.boost.org/trac10/ticket/5408#comment:5 <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">wontfix</span> </li> </ul> Ticket