Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:04:04+00:00 2026-06-03T23:04:04+00:00

This question seems to have been asked numerous times before, for example here1 ,

  • 0

This question seems to have been asked numerous times before, for example here1, here2 and here3.

What I am trying to do is, set the member function of the C-Struct gsl_function to a member function of my class.

class MyClass{

    double foo(double x)
    {
        return ...;
    }

    double bar(double x)
    {
        ...
        gsl_function F;
        // Problem I cant do this (compiler error)
        F.function = &(this->foo);
    }
};

The third link above provides a solution, I think it is based on the wrapper approach described here4.

So my question is can I do better. Is there an easier way? For example, possibly by using Boost’s function and Bind objects.

I am weighing up the option of using a gsl wrapper, such as o2scl. But am a bit releuctant as I may pay the price later if the wrapper is not well maintained. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:04:06+00:00Added an answer on June 3, 2026 at 11:04 pm

    Since GSL allows you to pass in arbitrary parameters, you can abuse this to hold the pointer to the instance under question. Then use a static member function to forward to the member function:

    class MyClass
    {
        double foo(double x)
        {
           ...
        }
        static double foo_wrapper(double x, void *params)
        {
            return static_cast<MyClass*>(params)->foo(x);
        }
    
        double bar(double x)
        {
            ...
            gsl_function F;
            F.function=&MyClass::foo_wrapper;
            F.params=this;
    
            // invoke GSL function passing in F
            ...
        }
    };
    

    Can you do better? Is there an easier way? Not really. Any approach you take will be doing this under the covers somewhere.

    But you can write a simple wrapper which hides some of this:

    class gsl_function_pp : public gsl_function
    {
    public:
        gsl_function_pp(boost::function<double(double)> const& func) : _func(func)
        {
            function=&gsl_function_pp::invoke;
            params=this;
        }
    private:
        boost::function<double(double)> _func;
    
        static double invoke(double x, void *params)
        {
            return static_cast<gsl_function_pp*>(params)->_func(x);
        }
    };
    

    This should give you (possibly at a moderate performance penalty due to multiple indirections involved) the type of functionality you’d want:

    class MyClass
    {
        double foo(double x)
        {
            ...
        }
    
        double bar(double x)
        {
            gsl_function_pp F(boost::bind(&MyClass::foo, this, _1));
            // invoke GSL function passing in F
            ...
        }
    };
    

    The caveat is that you’ll have to ensure that any gsl_function_pp object stays in scope for the entire time that the GSL might invoke it. So, don’t try to set up a root finder/etc in one function (using a local gsl_function_pp), return, and then perform root finding iterations in another — you’ll get a crash or worse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question seems to have been asked many times in many different forms but
This question seems to have been asked a lot but all were trying to
I've searched SO and this question seems to have been asked multiple times, but
This question has been asked many times before but it seems everyone else is
Variations of this question have been asked before, but it seems like the issue
This question seems to have been asked before, but I feel like my situation
This question seems to have been asked a lot, but I haven't seen an
I have asked this question ever before and it seems I didn't 100% resolved
I know that this question have been asked several times. But I can't get
i know that this is a famous question and have been asked many times.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.