Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function from another one

Tags:

c++

c

gsl

more than a general case, I have a very specific example in mind : in GSL (GNU Scientific Library), the main function type used (in order to perform integration, root finding,...) is gsl_function , which have an attribute function whose type is double(*)(double, void *)

Say I want to create a gsl_function from double a_squared(double a) {return a*a};. a__squared 's type is double(*)(double) I would like to create a convert function taking in argument (double(*)(double) f) and returning an object of type double(*)(double, void *) which would satisfy convert(f)(double a, NULL) == f(a)

But after some research, it seems like I can't define another function in my convert function. How to proceed ?

like image 566
user3640096 Avatar asked Oct 26 '15 15:10

user3640096


People also ask

How do I call a function from another Python file?

Approach: 1 Create a Python file containing the required functions. 2 Create another Python file and import the previous Python file into it. 3 Call the functions defined in the imported file.

How to create a function from a component in HTML?

We can create a function inside the “first.component.ts” file and call from “first.component.html” file. We can modify the “app-routing.module.ts” file.

How do you call a function from two components in Java?

We can run the application. We can click the first component link. We can click the “Call Function” button. It will call the function in the first component. Now we can click the second component link and click the “Call First Component Function” button.

How to execute a function from another component in Salesforce?

Now we can click the second component link and click the “Call First Component Function” button. It will execute the function from the first component and will show the same message box as given below. We have successfully executed the function from another component. We can even pass any data through this event emitter.


1 Answers

The need to pass a raw function pointer to the GSL API limits your options considerably - you can't use anything based on std::function because there's no way to obtain a function pointer from a std::function (and this rules out lambdas using captures, which would have offered a neat solution).

Given these constraints, here's a possible solution making use of a static wrapper class. You could just as well have put the contents of this class in a namespace, but using the class at least gives some semblance of encapsulation.

typedef double gsl_function_type(double, void*);  // typedef to make things a bit more readable...

// static class to wrap single-parameter function in GSL-compatible interface
// this really just serves as a namespace - there are no non-static members,
// but using a class lets us keep the details private
class Convert
{
    Convert() = delete;  // don't allow construction of this class    

    // pointer to the function to be invoked
    static double (*m_target)(double);

    // this is the function we'll actually pass to GSL - it has the required signature
    static double target(double x, void*) {
        return m_target(x);  // invoke the currently wrapped function
    }

public:
    // here's your "convert" function
    static gsl_function_type* convert(double (*fn)(double)) {
        m_target = fn;
        return ⌖
    }
};

There's a live example here: http://coliru.stacked-crooked.com/a/8accb5db47a0c51d

like image 163
atkins Avatar answered Sep 27 '22 22:09

atkins