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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:22:34+00:00 2026-05-27T02:22:34+00:00

So imagine we had 2 functions (void : ( void ) ) and (std::string

  • 0

So imagine we had 2 functions (void : ( void ) ) and (std::string : (int, std::string)) and we could have 10 more. All (or some of them) take in different argument types and can return different types. We want to store them in a std::map, so we get an API like this:

//Having a functions like:
int hello_world(std::string name, const int & number )
{
    name += "!";
    std::cout << "Hello, " << name << std::endl;
    return number;
}
//and
void i_do_shadowed_stuff()
{
    return;
}

//We want to be capable to create a map (or some type with similar API) that would hold our functional objects. like so:
myMap.insert(std::pair<std::string, fun_object>("my_method_hello", hello_world) )
myMap.insert(std::pair<std::string, fun_object>("my_void_method", i_do_shadowed_stuff) )
//And we could call tham with params if needed:
int a = myMap["my_method_hello"]("Tim", 25);
myMap["my_void_method"];

I wonder how to put many different functions into the same container. Specifically, how to do this in C++03 using Boost.

The API should be independent from the actual function types (having int a = myMap["my_method_hello"]("Tim", 25); not int a = myMap<int, (std::string, int)>["my_method_hello"]("Tim", 25);).

  • 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-27T02:22:34+00:00Added an answer on May 27, 2026 at 2:22 am
    #include <functional>
    #include <iostream>
    #include <string>
    #include <map>
    
    class api {
        // maps containing the different function pointers
        typedef void(*voidfuncptr)();
        typedef int(*stringcrintptr)(std::string, const int&);
    
        std::map<std::string, voidfuncptr> voida;
        std::map<std::string, stringcrintptr> stringcrint;
    public:
        // api temp class
        // given an api and a name, it converts to a function pointer
        // depending on parameters used
        class apitemp {
            const std::string n;
            const api* p;
        public:
            apitemp(const std::string& name, const api* parent)
                : n(name), p(parent) {}
            operator voidfuncptr()
            { return p->voida.find(n)->second; }
            operator stringcrintptr()
            { return p->stringcrint.find(n)->second; }
        };
    
        // insertion of new functions into appropriate maps
        void insert(const std::string& name, voidfuncptr ptr)
        { voida[name]=ptr; }
        void insert(const std::string& name, stringcrintptr ptr)
        { stringcrint[name]=ptr; }
        // operator[] for the name gets halfway to the right function
        apitemp operator[](std::string n) const
        { return apitemp(n, this); }
    };
    

    Usage:

    api myMap; 
    
    int hello_world(std::string name, const int & number )
    {
        name += "!";
        std::cout << "Hello, " << name << std::endl;
        return number;
    }
    
    int main()
    {
        myMap.insert("my_method_hello", &hello_world );
        int a = myMap["my_method_hello"]("Tim", 25);
    }
    

    Not very pretty. Better advice is to not do anything even remotely like whatever it is you’re trying to do.

    Note that this requires all functions with the same parameters to return the same type.

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

Sidebar

Related Questions

Imagine I have String in C#: I Don’t see ya.. I want to remove
Imagine that I had a form where I wanted to have a rent date
Imagine in the Global.asax.cs file I had an instance class as a private field.
Imagine we have a program trying to write to a particular file, but failing.
Imagine you want to animate some object on a WinForm. You setup a timer
Imagine the scene, you're updating some legacy Sybase code and come across a cursor.
Imagine I have an function which goes through one million/billion strings and checks smth
Imagine that you have a simple site with only 2 pages: login.aspx and secret.aspx.
Imagine I had a list of names, each paired with a value (C for
Imagine I was able to shuffle all numbers between 0 and 2^32 using something

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.