Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a duplicate question. See <a href="https://stackoverflow.com/questions/19168492/using-gsl-functions-defined-in-a-structure">Q1</a> or <a href="https://stackoverflow.com/questions/13074756/how-to-avoid-static-member-function-when-using-gsl-with-c">Q2</a> for example. Your problem is the following: you cannot convert pointers to member functions to free function pointers. To solve your problem, there are two options. You can define your member function as static (which is bad in 90% of the case because the member function will not be attached to any instantiation of your class and that is why you can convert it to a free function) or you can use the wrapper <a href="https://stackoverflow.com/a/18181494/2472169">you linked</a> that will use a static member functions under the hood to make your code compatible with gsl without the need of declaring your particular member function static.</p> <p>EDIT @Florian Oswald. Basically your entire solution can be implemented in 2 lines using std::bind the wrapper I cited before</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>In practice is this is just 1 extra line from a pure C code!</p> <p>As I stated in a comment, wrapping every member function that you want to integrate using an extra global struct and an extra global function is cumbersome and pollute your code with a lot of extra functions/struct that are completely unnecessary. Why use c++ if we refuse to use the features that make C++ powerful and useful (in comparison to C)? </p> <p>Another classical Example: if you want to pass a LOT of parameters, use lambda functions (no extra struct or global functions) !!! </p> <p>To be more precise: Imagine you have 2 parameters (doubles) . </p> <pre><code> //Declare them (locally) here double a1 = ...; double a2 = ...; // Declare a lambda function that capture all of them by value or reference // no need to write another struct with these 2 parameters + class pointer auto ptr = [&amp;](double x)-&gt;double {/.../}; // Cast to GSL in 3 lines using the wrapper std::function&lt;double(double)&gt; F1(ptr); gsl_function_pp F2(F1); gsl_function *F = static_cast&lt;gsl_function*&gt;(&amp;F2); </code></pre> <p>No extra global struct of global functions and no extra wrapper (the same wrapper that solved the problem of integrating member function also solved the problem of integrating a lambda expression). Of course this is a matter of style in the end, but in the absence of these nice features that allow the use of C libraries without code bloat, I would never leave C.</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. This table or related slice is empty.
    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