Boost C++ Libraries: Ticket #2642: bug constructing from a reference to another boost::function https://svn.boost.org/trac10/ticket/2642 <p> The following code throws a bad_function_call exception because the constructor for f2 decides that since f1 is empty, f2 ought to be empty as well. </p> <pre class="wiki">#include &lt;boost/function.hpp&gt; #include &lt;boost/ref.hpp&gt; void f() { } int main() { boost::function&lt;void()&gt; f1; boost::function&lt;void()&gt; f2(boost::ref(f1)); f1 = f; f2(); } </pre><p> The attached patch fixes the problem. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2642 Trac 1.4.3 Steven Watanabe Thu, 08 Jan 2009 01:53:33 GMT attachment set https://svn.boost.org/trac10/ticket/2642 https://svn.boost.org/trac10/ticket/2642 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">function_template.hpp.patch</span> </li> </ul> Ticket Daniel James Fri, 03 Jul 2009 22:20:27 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2642#comment:1 https://svn.boost.org/trac10/ticket/2642#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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/54616" title="When copying boost::ref, copy even when the referenced function is ...">[54616]</a>) When copying boost::ref, copy even when the referenced function is empty. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2642" title="#2642: Bugs: bug constructing from a reference to another boost::function (closed: fixed)">#2642</a> </p> <p> Patch by Steven Watanabe </p> Ticket Daniel James Wed, 08 Jul 2009 23:23:54 GMT <link>https://svn.boost.org/trac10/ticket/2642#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2642#comment:2</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/54824" title="Merge various function changes from trunk. Merged revisions ...">[54824]</a>) Merge various function changes from trunk. </p> <p> Merged revisions 49571,50064,51743,51745,53722,54616-54619 via svnmerge from <a class="ext-link" href="https://svn.boost.org/svn/boost/trunk"><span class="icon">​</span>https://svn.boost.org/svn/boost/trunk</a> </p> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/49571" title="Both Sun and Pgi on Linux correctly put typeinfo into the std ...">r49571</a> | noel_belcourt | 2008-11-03 18:37:49 +0000 (Mon, 03 Nov 2008) | 9 lines </p> </blockquote> <p> </p> <blockquote> <p> Both Sun and Pgi on Linux correctly put typeinfo into the std namespace, but function_base keys off the BOOST_NO_EXCEPTION_STD_NAMESPACE macro instead of the BOOST_NO_STD_TYPEINFO macro. The attached patch changes function_base to use the typeinfo macro. Because eVC 4.2 doesn't put typeinfo into the std namespace, I need to define BOOST_NO_STD_TYPEINFO only for this eVC version. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/50064" title="Fix -Wundef warning and suspect usage of BOOST_STRICT_CONFIG.">r50064</a> | johnmaddock | 2008-12-02 10:10:46 +0000 (Tue, 02 Dec 2008) | 1 line </p> </blockquote> <p> </p> <blockquote> <p> Fix -Wundef warning and suspect usage of BOOST_STRICT_CONFIG. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/51743" title="Implement an optimization that David Abrahams and myself came up with, ...">r51743</a> | dgregor | 2009-03-13 05:23:53 +0000 (Fri, 13 Mar 2009) | 11 lines </p> </blockquote> <p> </p> <blockquote> <p> Implement an optimization that David Abrahams and myself came up with, where Boost.Function uses a bit in the vtable pointer to indicate when the target function object has a trivial copy constructor, trivial destructor, and fits within the small object buffer. In this case, we just copy the bits of the function object rather than performing an indirect call to the manager. </p> </blockquote> <p> </p> <blockquote> <p> This results in a 60% speedup on a micro-benchmark that copies and calls such function objects repeatedly. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/51745" title="Make Boost.Function compile under BOOST_NO_EXCEPTIONS. Fixes #2499 ...">r51745</a> | dgregor | 2009-03-13 05:49:02 +0000 (Fri, 13 Mar 2009) | 7 lines </p> </blockquote> <p> </p> <blockquote> <p> Make Boost.Function compile under BOOST_NO_EXCEPTIONS. </p> </blockquote> <p> </p> <blockquote> <p> Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2499" title="#2499: Bugs: boost/function/function_template.hpp broken for -fno-exceptions. (closed: fixed)">#2499</a> Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2494" title="#2494: Bugs: compile errors in function_template.hpp when BOOST_NO_EXCEPTIONS defined (closed: fixed)">#2494</a> Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2469" title="#2469: Bugs: Build error in function_template.hpp if BOOST_NO_EXCEPTIONS is defined (closed: fixed)">#2469</a> Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2466" title="#2466: Bugs: function_template.hpp uses exceptions even when BOOST_NO_EXCEPTIONS is ... (closed: fixed)">#2466</a> </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/53722" title="Make Boost.Function compile with disabled exceptions. Closes #2900. ...">r53722</a> | vladimir_prus | 2009-06-07 16:44:50 +0100 (Sun, 07 Jun 2009) | 4 lines </p> </blockquote> <p> </p> <blockquote> <p> Make Boost.Function compile with disabled exceptions. </p> </blockquote> <p> </p> <blockquote> <p> Closes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2900" title="#2900: Bugs: Function Library does not compile with no excpetions (closed: fixed)">#2900</a>. Patch from Gabi Davar. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/54616" title="When copying boost::ref, copy even when the referenced function is ...">r54616</a> | danieljames | 2009-07-03 23:20:26 +0100 (Fri, 03 Jul 2009) | 3 lines </p> </blockquote> <p> </p> <blockquote> <p> When copying boost::ref, copy even when the referenced function is empty. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2642" title="#2642: Bugs: bug constructing from a reference to another boost::function (closed: fixed)">#2642</a> </p> </blockquote> <p> </p> <blockquote> <p> Patch by Steven Watanabe </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/54617" title="Add 'and later versions' to support info for GCC and Visual C++. Fixes ...">r54617</a> | danieljames | 2009-07-03 23:20:52 +0100 (Fri, 03 Jul 2009) | 6 lines </p> </blockquote> <p> </p> <blockquote> <p> Add 'and later versions' to support info for GCC and Visual C++. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2847" title="#2847: Bugs: Additional compatible compilers for preferred syntax (closed: fixed)">#2847</a>. </p> </blockquote> <p> </p> <blockquote> <p> I didn't explicitly specify the versions since no one's updating this list and it's highly unlikely that a future version will break this. The same could probably be done for the other compilers but I don't know them very well so I'm leaving them alone. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/54618" title="Fix Boost.Function unit tests for C++0x. Fixes #3012 Based on a patch ...">r54618</a> | danieljames | 2009-07-03 23:21:40 +0100 (Fri, 03 Jul 2009) | 4 lines </p> </blockquote> <p> </p> <blockquote> <p> Fix Boost.Function unit tests for C++0x. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3012" title="#3012: Bugs: [function] Test failures with GCC 4.3/4.4 in C++0x mode (closed: fixed)">#3012</a> </p> </blockquote> <p> </p> <blockquote> <p> Based on a patch from Richard Webb. Changed a bit so that it also works for the Visual C++ 10 beta. </p> </blockquote> <p> ........ </p> <blockquote> <p> <a class="changeset" href="https://svn.boost.org/trac10/changeset/54619" title="Work around Visual C++ copy constructor bug. Fixes #2929. Based on ...">r54619</a> | danieljames | 2009-07-03 23:22:03 +0100 (Fri, 03 Jul 2009) | 3 lines </p> </blockquote> <p> </p> <blockquote> <p> Work around Visual C++ copy constructor bug. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2929" title="#2929: Bugs: boost::function cause runtime stack overflow (closed: fixed)">#2929</a>. </p> </blockquote> <p> </p> <blockquote> <p> Based on the patch by Steven Watanabe. </p> </blockquote> <p> ........ </p> </description> <category>Ticket</category> </item> <item> <author>Yusaku Sugai <sugai@…></author> <pubDate>Wed, 02 Sep 2009 02:43:29 GMT</pubDate> <title>status changed; resolution deleted https://svn.boost.org/trac10/ticket/2642#comment:3 https://svn.boost.org/trac10/ticket/2642#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">fixed</span> </li> </ul> <p> The following code throws a bad_function_call exception with Boost 1.40.0.<br /> It was OK with Boost 1.38.0.<br /> I suppose <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2642" title="#2642: Bugs: bug constructing from a reference to another boost::function (closed: fixed)">#2642</a> patch made reference to function(empty or not) to be always non-empty.<br /> </p> <pre class="wiki">#include &lt;boost/function.hpp&gt; #include &lt;boost/ref.hpp&gt; int main() { boost::function&lt;void()&gt; f1; boost::function&lt;void()&gt; f2(boost::ref(f1)); if (f2) { f2(); } } </pre> Ticket Steven Watanabe Wed, 02 Sep 2009 03:18:22 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2642#comment:4 https://svn.boost.org/trac10/ticket/2642#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> This behavior is correct. f2 is not empty. That what it holds happens to be a reference to an empty boost::function is irrelevant. BTW, before 1.40, you could get a bad_function_call with </p> <pre class="wiki">#include &lt;boost/function.hpp&gt; #include &lt;boost/ref.hpp&gt; void f() {} int main() { boost::function&lt;void()&gt; f1(&amp;f); boost::function&lt;void()&gt; f2(boost::ref(f1)); f1.clear(); if (f2) { f2(); } } </pre> Ticket Yusaku Sugai <sugai@…> Wed, 02 Sep 2009 04:53:37 GMT <link>https://svn.boost.org/trac10/ticket/2642#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2642#comment:5</guid> <description> <p> I understand that in a sense f2 is not empty.<br /> I expected that<br /> </p> <blockquote> <p> f.empty() is false =&gt; It's OK to call f().<br /> f.empty() is true =&gt; It's not OK to call f().<br /> </p> </blockquote> <p> But it is not what empty()-ness means in function library, right?<br /> </p> <p> Is there any way to know in advance whether it is OK to call a function object or not?<br /> Do I have to just try calling it and be ready for bad_function_call exception? </p> </description> <category>Ticket</category> </item> </channel> </rss>