Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think you actually need to specialize for void return type. A void function is allowed to return the "result" of another void function for exactly this scenario.</p> <pre><code>void foo() { } void bar() { return foo(); } //this is OK int main() { bar(); } </code></pre> <p>So your only problem would be how to determine the return type.</p> <p>It appears that <code>boost::function</code> has a typedef for <code>result_type</code> (see <a href="http://beta.boost.org/doc/libs/1_37_0/doc/html/boost/functionN.html" rel="nofollow noreferrer">http://beta.boost.org/doc/libs/1_37_0/doc/html/boost/functionN.html</a>)</p> <pre><code>#include &lt;boost/function.hpp&gt; template&lt;class T&gt; class Container { public: typedef typename boost::function&lt;T&gt;::result_type result_type; private: boost::function&lt;T&gt; f; }; Container&lt;bool(int, int)&gt;::result_type r = true; </code></pre> <hr> <p>Edit: Now that you know what the <code>result_type</code> is, and you do need to distinguish between void/non-void results, you can employ <code>enable_if</code> and <code>disable_if</code>. The only complication is that those only work with function templates, so a non-template <code>foo</code> calls a templated <code>do_foo</code>.</p> <pre><code>#include &lt;boost/function.hpp&gt; #include &lt;boost/utility/enable_if.hpp&gt; #include &lt;boost/type_traits.hpp&gt; #include &lt;cstdio&gt; template&lt;class T&gt; class Container { public: typedef typename boost::function&lt;T&gt;::result_type result_type; result_type foo() { return do_foo&lt;result_type&gt;(); //note that this still works because you can return the void result! :) } private: //use this if the result_type is void template &lt;class U&gt; typename boost::enable_if&lt;boost::is_same&lt;U, void&gt;, U &gt;::type do_foo() { std::puts("for void"); } //else template &lt;class U&gt; typename boost::disable_if&lt;boost::is_same&lt;U, void&gt;, U&gt;::type do_foo() { std::puts("other"); return U(); } private: boost::function&lt;T&gt; f; }; int main() { Container&lt;void()&gt; a; a.foo(); Container&lt;int()&gt; b; b.foo(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload