Boost C++ Libraries: Ticket #301: boost::function doesn't accept classes declared in functions https://svn.boost.org/trac10/ticket/301 <pre class="wiki">Submitter: John Engström &lt;john.engstrom\"\s\p\a\m\"@\"\s\p\a\m\"solcorp.com&gt; Using MSVC 7.1, I use a functor defined in-scope, as in the following example. ///////////////////// code snippet void bar() { ... struct my_functor { void operator () (int arg1, int arg2) { ... foo(); ... } ... }; my_functor my_functor_instance; boost::function2&lt;void, int, int&gt; my_function1 = boost::ref(my_functor_instance); boost::function2&lt;void, int, int&gt; my_function2 = my_functor_instance; } /////////////////// explanation This fails to compile, see below for error messages, however if I move the declaration of the functor out of the function bar(), it compiles fine. Perhaps it only happens with MSVC. /////////////////// error messages main.cpp(116): error C2893: Failed to specialize function template 'const boost::reference_wrapper&lt;T&gt; boost::ref(T &amp;)' main.cpp(117): error C2440: 'initializing' : cannot convert from 'WinMain::my_functor' to 'boost::function2&lt;R,T0,T1&gt;' with [ R=void, T0=int, T1=int ] </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/301 Trac 1.4.3 Vladimir Prus Thu, 09 Sep 2004 13:59:33 GMT <link>https://svn.boost.org/trac10/ticket/301#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/301#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=321498 To begin with, it seems it's boost::ref which fails. I believe this will never work, on any compiler, because local classes are not valid template arguments. What do you think? </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Douglas Gregor</dc:creator> <pubDate>Thu, 09 Sep 2004 14:03:09 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/301#comment:2 https://svn.boost.org/trac10/ticket/301#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> <pre class="wiki">Logged In: YES user_id=249098 Vladimir is correct: local classes are not valid template arguments, so it is impossible for boost::function (or boost::ref) to support this usage (although I wish it could!). </pre> Ticket nobody Thu, 09 Sep 2004 14:12:47 GMT <link>https://svn.boost.org/trac10/ticket/301#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/301#comment:3</guid> <description> <pre class="wiki">Logged In: NO Thanks for the swift response and "ouch." :) I had really been fantisising about local classes being exactly equivalent to global/namespaced/nested ones. Thanks again! John Engstrom </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>ossmdw45</dc:creator> <pubDate>Thu, 09 Sep 2004 14:52:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/301#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/301#comment:4</guid> <description> <pre class="wiki">Logged In: YES user_id=1108669 &gt; Summary: boost::function doesn't accept classes declared in functions &gt; This fails to compile, see below for error messages, &gt; however if I move the declaration of the functor out of &gt; the function bar(), it compiles fine. Perhaps it only &gt; happens with MSVC. No, that's a limitation of C++ templates. Only classes/structs with external linkage can be used as template arguments, IIRC. I've heard people talking about removing this restriction for specifically the reason you're talking about: to be able to define "lambda" functions in-scope. But right now it's illegal. You _can_, however, define it this way (code not compiled since I'm at work): void bar() { ... struct localfunction { static void foo(int arg1, int arg2) { moo(); } ... }; boost::function2&lt;void, int, int&gt; my_function1 = boost::ref(localfunction::foo); boost::function2&lt;void, int, int&gt; my_function2 = localfunction::foo; } This works (should work) because localfunction::foo is just a regular function pointer (int*int-&gt;void), so you can templatize on it. </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>nobody</dc:creator> <pubDate>Wed, 29 Sep 2004 15:14:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/301#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/301#comment:5</guid> <description> <pre class="wiki">Logged In: NO Yes! Cool technique Max ! With some macros, I was able to make boost::function&lt;&gt; objects out of my local functors.. John Engstrom </pre> </description> <category>Ticket</category> </item> </channel> </rss>