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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:45:40+00:00 2026-06-18T15:45:40+00:00

For a library, I’d like a function to accept another function and its arguments,

  • 0

For a library, I’d like a function to accept another function and its arguments, then store them all for calling later. The arguments must allow for any mixture of types, but the functions only need to return void. Something like this:

void myFunc1(int arg1, float arg2);
void myFunc2(const char *arg1);
class DelayedCaller
{ ...
public:
    static DelayedCaller *setup(Function func, …);
};

...
DelayedCaller* caller1 = DelayedCaller::setup(&myFunc1, 123, 45.6);
DelayedCaller* caller2 = DelayedCaller::setup(&myFunc2, "A string");

caller1->call(); // Calls myFunc1(), with arguments 123 and 45.6
caller2->call(); // Calls myFunc2(), with argument "A string"

One approach is to make DelayedCaller::setup() accept a std::function, and have my library users use std::bind() prior to calling setup(). However, is there a way to implement setup() such that users don’t need to do the binding themselves?

Edit: DelayedCaller is an existing class. setup() is a new static method I’d like to add.

  • 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-18T15:45:41+00:00Added an answer on June 18, 2026 at 3:45 pm

    An possibility is to use variadic templates and call std::bind() from within the setup() function:

    #include <iostream>
    #include <string>
    #include <functional>
    #include <memory>
    
    void myFunc1(int arg1, float arg2)
    {
        std::cout << arg1 << ", " << arg2 << '\n';
    }
    void myFunc2(const char *arg1)
    {
        std::cout << arg1 << '\n';
    }
    
    class DelayedCaller
    {
    public:
        template <typename TFunction, typename... TArgs>
        static std::unique_ptr<DelayedCaller> setup(TFunction&& a_func,
                                                    TArgs&&... a_args)
        {
            return std::unique_ptr<DelayedCaller>(new DelayedCaller(
                std::bind(std::forward<TFunction>(a_func),
                          std::forward<TArgs>(a_args)...)));
        }
        void call() const { func_(); }
    
    private:
        using func_type = std::function<void()>;
        DelayedCaller(func_type&& a_ft) : func_(std::forward<func_type>(a_ft)) {}
        func_type func_;
    };
    
    int main()
    {
        auto caller1(DelayedCaller::setup(&myFunc1, 123, 45.6));
        auto caller2(DelayedCaller::setup(&myFunc2, "A string"));
    
        caller1->call();
        caller2->call();
    
        return 0;
    }
    

    Output:

    123, 45.6
    A string
    

    Return a smart pointer, such as std::unique_ptr, instead of returning a raw pointer (or return by value and avoid dynamic allocation. The func_type is moveable if the arguments are moveable, or it may be quite cheap to copy anyway. You may need to define the move constructor and move assignment operator, they are generated under certain conditions).

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

Sidebar

Related Questions

As library docs say CString created with newCString must be freed with free function.
JS library like JQuery can be linked directly from other site (e.g. google). Usually
A library function parses a file and returns an object. If a parser encounters
library(igraph) g=graph.famous(Zachary) c=walktrap.community(g) a=community.to.membership(g,c$merges,steps=2) b=a$membership modularity(g,b) When running the modularity(g,b) function, R did not
LINQ library in .NET framework does have a very useful function called GroupBy ,
What library does Firefox use for its user interface and can you use it
My library needs to read-in big-endian integers (4-bytes) and convert them to the endian
OpenCV library version 2.42. I'd like to set a parameter in BackgroundSubtractorMOG2 object, e.g.
Which library I have to use to work with archives(like rar, zip) on android.
library(quantmod) getSymbols('AAPL') n <- nrow(AAPL) a <- runif(n) I would like to convert a

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.