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 6240229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:31:46+00:00 2026-05-24T11:31:46+00:00

I would like to try something out and unify some boilerplate code in one

  • 0

I would like to try something out and unify some boilerplate code in one of our dynamic library API wrappers.

Essentially, I would like to do the following:

typedef bool (*MyFPtrT)(long id, std::string const& name);
typedef boost::function<bool (long id, std::string const& name)> MyFObjT;
...

...
  MyFPtrT pDllFun = NULL;
  long x = 42; string s = "Answer"; // API input, not hardcoded
  MyFObjT f = boost::bind(pDllFun, x, s);
  ...
  return Call(f);
...

template<FT>
bool Call(FT f) {
  ...
  MyFPtrT pDllFun = (MyFunPtr)::GetProcAddress(...);
  f.setFunctionPointerButLeaveBoundParameters(pDllFun); // <- how??
  // Now call the correctly rigged up function object:
  return f();
}

Is this possible? (With Boost or otherwise?) (C++03)

  • 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-05-24T11:31:46+00:00Added an answer on May 24, 2026 at 11:31 am

    I don’t think it can be done directly like that, because bind creates a new object that takes the function object and the arguments by reference, and you can’t rebind references.

    However, you can easily enough write your own templated functor wrapper with a reassignable function pointer:

    template <typename R, typename A, typename B>
    struct ReAssFunctor
    {
      typedef R(*FP)(A, B);
    
      ReAssFunctor(const A & a_, const B & b_) a(a_), b(b_) { }
    
      R operator()() const { return func(a, b); }
    
      FP function;
    
    private:
      const A & a;
      const B & b;
    };
    

    [Edit:] Do check the comments below; holding references taken by const reference in the constructor can be dangerously abused, so be careful, or save a and b by value if you prefer.[/]

    Now you can initialize the functor with the arguments early:

    ReAssFunctor<R, A, B> raf(a, b);
    

    Then assign a function pointer and call:

    raf.function = foo;
    raf();
    
    raf.function = goo;
    raf();
    

    You could even replace the call operator by this:

      template <typename U>
      U operator()(U (f*)(A, B)) const { return f(a, b); }
    

    And then invoke the functor with the function pointer as the argument:

    raf(fp1);  // calls fp1(a,b);
    raf(fp2);  // calls fp2(a,b);
    

    In that case you don’t need the return type as a fixed parameter any longer, but now you don’t get a nullary call operator. Take your pick.

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

Sidebar

Related Questions

I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
I would like to try this code: public struct Direction { private int _azimuth;
I have an OS design project with some subprojects, I would like to try
I'm a pretty big noob to Java, but I would like try out htmlunit.
I've recently started looking at the Google Maps API to try out something new
I want to try out some of the MySQL software, like Workbench, on the
I would like to try programming genie , but I can't find neither source
I built a slideshow/decision-making game in Flash but would like to try to redo
I would like to to try Emacs, and want to give it the best
I would like to implement an B+ tree in Java and try to optimize

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.