Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to avoid static member function when using gsl with c++
    text
    copied!<p>I would like to use GSL within a c++ class without declaring member functions as <code>static</code>. The reason for this is because I don't know them too well and I'm not sure about thread safety. From what I read, <code>std::function</code> might be a solution but I'm not sure how to use it.</p> <p>My question comes down to how can I remove <code>static</code> in declaration of <code>g</code>?</p> <pre><code>#include&lt;iostream&gt; #include &lt;functional&gt; #include &lt;stdlib.h&gt; #include &lt;gsl/gsl_math.h&gt; #include &lt;gsl/gsl_monte.h&gt; #include &lt;gsl/gsl_monte_plain.h&gt; #include &lt;gsl/gsl_monte_miser.h&gt; #include &lt;gsl/gsl_monte_vegas.h&gt; using namespace std; class A { public: static double g (double *k, size_t dim, void *params) { double A = 1.0 / (M_PI * M_PI * M_PI); return A / (1.0 - cos (k[0]) * cos (k[1]) * cos (k[2])); } double result() { double res, err; double xl[3] = { 0, 0, 0 }; double xu[3] = { M_PI, M_PI, M_PI }; const gsl_rng_type *T; gsl_rng *r; ////// the following 3 lines didn't work /////// //function&lt;double(A,double*, size_t, void*)&gt; fg; //fg = &amp;A::g; //gsl_monte_function G = { &amp;fg, 3, 0 }; gsl_monte_function G = { &amp;g, 3, 0 }; size_t calls = 500000; gsl_rng_env_setup (); T = gsl_rng_default; r = gsl_rng_alloc (T); { gsl_monte_plain_state *s = gsl_monte_plain_alloc (3); gsl_monte_plain_integrate (&amp;G, xl, xu, 3, calls, r, s, &amp;res, &amp;err); gsl_monte_plain_free (s); } gsl_rng_free (r); return res; } }; main() { A a; cout &lt;&lt;"gsl mc result is " &lt;&lt; a.result() &lt;&lt;"\n"; } </code></pre> <p><strong>Update (1)</strong>:</p> <p>I tried changing <code>gsl_monte_function G = { &amp;g, 3, 0 };</code> to <code>gsl_monte_function G = { bind(&amp;A::g, this,_1,_2,_3), 3, 0 };</code> but it didn't work</p> <p><strong>Update (2)</strong>: I tried using <a href="https://stackoverflow.com/questions/12833707/assigning-stdfunction-to-a-member-function">assigning std::function to a member function</a> but it didn't work either.</p> <p><strong>Update (3)</strong> in the end I wrote a non-member function:</p> <pre><code>double gmf (double *k, size_t dim, void *params) { auto *mf = static_cast&lt;A*&gt;(params); return abs(mf-&gt;g(k,dim,params)); //return 1.0; }; </code></pre> <p>It worked but it's a messy solution because I needed to write a helper function. With lambdas,function and bind, there should be a way to have everything logical within the class.</p>
 

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