Binding pointer-to-member-function to std::pair and casting to void*
<p>I have the following code snippet of a small Thread class I am trying to implement: </p>

<p>declaration:</p>

<pre><code>template &lt;typename T&gt;
class Thread{
public:
    Thread(T*, void (T::*)());
    void start();
private:
    friend void* createThread(void*);
    T* arg;  //the object
    void (T::*function)(); //the ptr to function in that object of type T
    pthread_t thread;
};
</code></pre>

<p>And below is a snippet of the definition.</p>

<pre><code>template &lt;typename T&gt;
void* createThread(void *arg){
    //get back the pair..
    std::pair&lt;T*, void (T::*)()&gt;* p = (std::pair&lt;T*, void (T::*)()&gt;*)arg;

    //open up the pair
    T&amp; t = *p-&gt;first;
    void (T::*fn)() = p-&gt;second;

    //TEST
    Temp ttt;
    ttt.a=100;
    (ttt.*fn)(); //segfaults here..

    (t.*fn)(); //and even here
}

template &lt;typename T&gt;
void Thread&lt;T&gt;::start(){
    //pair of pointer to object, and the pointer-to-member-function
    std::pair&lt;T*, void (T::*)()&gt; p(arg,function);
    pthread_create(&amp;thread, NULL, createThread&lt;T&gt;, (void*)&amp;p);
}
</code></pre>

<p>In the above code, <code>Temp</code> is a class with a function and a field 'a'. And I get the thread running by the following code:</p>

<pre><code>Temp t;
t.a=11;
Thread&lt;Temp&gt; tt(&amp;t, &amp;Temp::function);

tt.start();
</code></pre>

<p>Any idea why the code segfaults? I recollect that pointer-to-member-function does not really go well with casting to void* and back. Is that the case in here (since I am not doing that directly)?</p>

<p>Any pointers/suggestions will be highly appreciated.</p>

<p>Thanks! :)</p>
