Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can easily wrap member functions using the following code (which is a well known solution)</p> <pre><code> class gsl_function_pp : public gsl_function { public: gsl_function_pp(std::function&lt;double(double)&gt; const&amp; func) : _func(func){ function=&amp;gsl_function_pp::invoke; params=this; } private: std::function&lt;double(double)&gt; _func; static double invoke(double x, void *params) { return static_cast&lt;gsl_function_pp*&gt;(params)-&gt;_func(x); } }; </code></pre> <p>Then you can use std::bind to wrap the member function in a std::function. Example:</p> <pre><code>gsl_function_pp Fp( std::bind(&amp;Class::member_function, &amp;(*this), std::placeholders::_1) ); gsl_function *F = static_cast&lt;gsl_function*&gt;(&amp;Fp); </code></pre> <p>However, you should be aware about the performance penalties of std::function before wrapping member functions inside gsl integration routine. See <a href="https://stackoverflow.com/questions/14677997/stdfunction-vs-template/15015502#15015502">template vs std::function </a> . To avoid this performance hit (which may or may not be critical for you), you should use templates as shown below </p> <pre><code>template&lt; typename F &gt; class gsl_function_pp : public gsl_function { public: gsl_function_pp(const F&amp; func) : _func(func) { function = &amp;gsl_function_pp::invoke; params=this; } private: const F&amp; _func; static double invoke(double x, void *params) { return static_cast&lt;gsl_function_pp*&gt;(params)-&gt;_func(x); } }; </code></pre> <p>In this case, to call a member function you need the following</p> <pre><code> Class* ptr2 = this; auto ptr = [=](double x)-&gt;double{return ptr2-&gt;foo(x);}; gsl_function_pp&lt;decltype(ptr)&gt; Fp(ptr); gsl_function *F = static_cast&lt;gsl_function*&gt;(&amp;Fp); </code></pre> <p>PS: the link <a href="https://stackoverflow.com/questions/14677997/stdfunction-vs-template/15015502#15015502">template vs std::function </a> explains that compiler usually has an easier time optimizing templates than std::function (which is critical for performance if your code is doing heavy numerical calculation). So even tough the workaround in the second example seems more cumbersome, I would prefer templates than std::function.</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.
 

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