Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>boost::function</code> allows anything with an <code>operator()</code> with the right signature to be bound as the parameter, and the result of your bind can be called with a parameter <code>int</code>, so it can be bound to <code>function&lt;void(int)&gt;</code>.</p> <p>This is how it works (this description applies alike for <code>std::function</code>):</p> <p><code>boost::bind(&amp;klass::member, instance, 0, _1)</code> returns an object like this</p> <pre><code>struct unspecified_type { ... some members ... return_type operator()(int i) const { return instance-&gt;*&amp;klass::member(0, i); } </code></pre> <p>where the <code>return_type</code> and <code>int</code> are inferred from the signature of <code>klass::member</code>, and the function pointer and bound parameter are in fact stored in the object, but that's not important</p> <p>Now, <code>boost::function</code> doesn't do any type checking: It will take any object and any signature you provide in its template parameter, and create an object that's callable according to your signature and calls the object. If that's impossible, it's a compile error.</p> <p><code>boost::function</code> is actually an object like this:</p> <pre><code>template &lt;class Sig&gt; class function { function_impl&lt;Sig&gt;* f; public: return_type operator()(argument_type arg0) const { return (*f)(arg0); } }; </code></pre> <p>where the <code>return_type</code> and <code>argument_type</code> are extracted from <code>Sig</code>, and <code>f</code> is dynamically allocated on the heap. That's needed to allow completely unrelated objects with different sizes bind to <code>boost::function</code>.</p> <p><code>function_impl</code> is just an abstract class</p> <pre><code>template &lt;class Sig&gt; class function_impl { public: virtual return_type operator()(argument_type arg0) const=0; }; </code></pre> <p>The class that does all the work, is a concrete class derived from <code>boost::function</code>. There is one for each type of object you assign to <code>boost::function</code></p> <pre><code>template &lt;class Sig, class Object&gt; class function_impl_concrete : public function_impl&lt;Sig&gt; { Object o public: virtual return_type operator()(argument_type arg0) const=0 { return o(arg0); } }; </code></pre> <p>That means in your case, the assignment to boost function:</p> <ol> <li>instantiates a type <code>function_impl_concrete&lt;void(int), unspecified_type&gt;</code> (that's compile time, of course)</li> <li>creates a new object of that type on the heap</li> <li>assigns this object to the f member of boost::function</li> </ol> <p>When you call the function object, it calls the virtual function of its implementation object, which will direct the call to your original function.</p> <p>DISCLAIMER: Note that the names in this explanation are deliberately made up. Any resemblance to real persons or characters ... you know it. The purpose was to illustrate the principles.</p>
    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