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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:44:21+00:00 2026-05-14T14:44:21+00:00

I have a C++ library that should expose some system\ resource calls as callbacks

  • 0

I have a C++ library that should expose some system\ resource calls as callbacks from the linked application. For example: the interfacing application (which uses this library) can send socket management callback functions – send, receive, open, close etc., and the library will use this implementation in stead of the library’s implementation.
(This way enables the application to manage the sockets by itself, can be useful).
This library has to expose also more callbacks, like, for example, a password validation, so I wonder if there is a preferred method to expose the callback sending option in one API.
Something like:

int AddCallbackFunc (int functionCallbackType, <generic function prototype>, <generic way to pass some additional arguments>)

Then within my library I will assign the callback to the appropriate function pointer according to the functionCallbackType parameter.

Is there any way to implement it in a generic way which will fit ANY function prototype and ANY additional arguments?

Your help will be more than appreciated…
Thanks!

  • 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-14T14:44:22+00:00Added an answer on May 14, 2026 at 2:44 pm

    Why not have it accept a 0 argument functor and just have the user use boost::bind to build the arguments into it before registering it? Basically example (calls instead of stores, but you get the point):

    #include <tr1/functional>
    #include <iostream>
    
    void callback(const std::tr1::function<int()> &f) {
        f();
    }
    
    int x() {
        std::cout << "x" << std::endl;
        return 0;
    }
    
    int y(int n) {
        std::cout << "y = " << n << std::endl;
        return 0;
    }
    
    int main(int argc, char *argv[]) {
        callback(x);
        callback(std::tr1::bind(y, 5));
    }
    

    EDIT: There is an option B, which is to basically implement what bind does under the hood with structures to store all the needed info and inheritance for polymorphism… it becomes a mess real quick. I would not recommend it, but it will work. You can also save from grief by forcing a return type of int, but that only saves you a little.

    #include <iostream>
    
    struct func_base {
        virtual int operator()() = 0;
    };
    
    // make one of these for each arity function you want to support (boost does this up to 50 for you :-P
    struct func0 : public func_base {
        typedef int (*fptr_t)();
    
        func0(fptr_t f) : fptr(f) {
        }
    
        virtual int operator()() { return fptr(); }
    
        fptr_t fptr;
    };
    
    // demonstrates an arity of 1, templated so it can take any type of parameter
    template <class T1>
    struct func1 : public func_base {
        typedef int (*fptr_t)(T1);
    
        func1(fptr_t f, T1 a) : fptr(f), a1(a) {
        }
    
        virtual int operator()() { return fptr(a1); }
    
        fptr_t fptr;
        T1 a1;
    };
    
    void callback(func_base *f) {
        (*f)();
    }
    
    int x() {
        std::cout << "x" << std::endl;
        return 0;
    }
    
    int y(int n) {
        std::cout << "y = " << n << std::endl;
        return 0;
    }
    
    int main(int argc, char *argv[]) {
        // NOTE: memory leak here...
        callback(new func0(x));
        callback(new func1<int>(y, 5));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.