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

  • Home
  • SEARCH
  • 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 8948325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:57:21+00:00 2026-06-15T12:57:21+00:00

Is there a way to store void functions with different parameters in a vector?

  • 0

Is there a way to store void functions with different parameters in a vector? The number of parameters is always one, only the type differs. The parameter type can be a default type like int, a pointer to my own object like example* or any other type.

What I do for now is using a vector of functions with a void pointer as parameter. So I can pass everything. But I want to get rid of the back casting in all the functions.

unordered_map<string, function<void(void*)> > List;

void Callback(string Name, function<void(void*)> Function)
{
    List[Name].push_back(&Function);
}

Callback("event", [](void* x){
    int value = *(int*)x;
    cout << value << endl;
});

Here is an example to illustrate what I would like to have. Please note the template syntax I would prefer. Therefore I would need to store all the functions in a container.

vector<function<void(...)> > List; // need something other than a std vector

template <typename T>
void Callback(string Name, function<void(T)> Function)
{
    List[Name].push_back(&Function);
}

Callback<int>([](int x){
    cout << x << endl;
});

This application is performance related since it is an essential part of a realtime rendering engine.

Edit: I solved the point of storing functions without parameters, so this is not part of the question anymore what makes the question more clear and straightforward.

  • 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-15T12:57:23+00:00Added an answer on June 15, 2026 at 12:57 pm

    I developed a void pointer based event system and it works now. It uses templates for passing and receiving data. Functions with none or one parameter of any type both are supported. Since it uses void pointers to store the callback functions I suppose it is very fast compared to a solution using the any type from boost framework.

    #include <string>
    #include <vector>
    #include <unordered_map>
    #include <functional> 
    #include <memory>
    
    using namespace std;
    
    class ManagerEvent
    {
        typedef unordered_map<string, unordered_map<int, vector<pair<void*, bool> > > > Events;
    public:
        void Listen(string Name, function<void()> Function)
        {
            Listen(Name, 0, Function);
        }
        void Listen(string Name, int State, function<void()> Function)
        {
            List[Name][State].push_back(make_pair(new function<void()>(Function), false));
        }
        template <typename T>
        void Listen(string Name, function<void(T)> Function)
        {
            Listen<T>(Name, 0, Function);
        }
        template <typename T>
        void Listen(string Name, int State, function<void(T)> Function)
        {
            List[Name][State].push_back(make_pair(new function<void(T)>(Function), true));
        }
        void Fire(string Name)
        {
            Fire(Name, 0);
        }
        void Fire(string Name, int State)
        {
            auto Functions = List[Name][State];
    
            for (auto i = Functions.begin(); i != Functions.end(); ++i)
            {
                if(i->second) continue;
                else          (*(function<void()>*)(i->first))();
            }
        }
        void FireRange(string Name, int From, int To)
        {
            for(int i = From; i <= To; ++i) Fire(Name, i);
        }
        template <typename T>
        void Fire(string Name, T Data)
        {
            Fire(Name, 0, Data);
        }
        template <typename T>
        void Fire(string Name, int State, T Data)
        {
            auto Functions = List[Name][State];
    
            for (auto i = Functions.begin(); i != Functions.end(); ++i)
            {
                if(i->second) (*(function<void(T)>*)i->first)(Data);
                else          (*(function<void()>*)i->first)();
            }
        }
    private:
        Events List;
    };
    

    This is what came to my mind and it works quite well. However, please feel free to suggest improvements or use the code for your own projects.

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

Sidebar

Related Questions

Is there any way to store the generic parameter type passed in at construction
Is there any way to store a session variable in OpenEdge SQL similar to
is there any way to store duplicate keys in a dictionary? I have a
Is there a way to store values on client side permanently? I have a
Is there a way to store the state of objects in GXT? That is,
Is there any way to store file creation time in linux? May be filesystem
is there a way to store a class name in localstorage ?? If there
Is there a JavaScript way to store data that is unique to a browser
is there any easy way of store XML data into core data? Currently, my
I'd like to know if there is any way to store a series of

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.