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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:05:42+00:00 2026-05-11T11:05:42+00:00

i was wondering if there was a way to do this in C++? void

  • 0

i was wondering if there was a way to do this in C++?

void func1(const std::string& s) { std::cout << s << std::endl; }  void func2(int me) { std::cout << me << std::endl; }  int main() { std::map<std::string, boost::function< ??? > > a_map;  a_map['func1'] = &func1; a_map['func1']('HELLO');  } 

Is there any way to do what i have above using boost function and a map?

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

    There are ways to store the functions, the problem is, in order to be able to call the function with the desired argument you’d have to know the calling signature of the function anyways, and if you have that information, you might as well use separate maps, or use a more complicated object than boost::function.

    If you’re willing to do a bit of work and have a finite number of signatures, you could just do something like this:

    class MultiFunc { protected:     MultiFunc() {}  public:     typedef void (*stringFunc)(const std::string&);     typedef void (*intFunc)(int);      static MultiFunc *Create(stringFunc function);     static MultiFunc *Create(intFunc function);      virtual void operator()(const string &) { throw exception(); }     virtual void operator()(int) { throw exception(); }     virtual ~MultiFunc(); };  class MultiFuncString : public MultiFunc { private:     stringFunc Function; public:     MultiFuncString(stringFunc function) : Function(function) {}     virtual void operator()(const string &arg) { Function(arg); } };  class MultiFuncInt : public MultiFunc { private:     intFunc Function; public:     MultiFuncInt(intFunc function) : Function(function) {}     virtual void operator()(int arg) { Function(arg); } };  MultiFunc *MultiFunc::Create(MultiFunc::stringFunc function) {     return new MultiFuncString(function); } MultiFunc *MultiFunc::Create(MultiFunc::intFunc function) {     return new MultiFuncInt(function); }  void func1(const std::string& s) { std::cout << s << std::endl; } void func2(int me) { std::cout << me << std::endl; }  int main() {     map<string, MultiFunc *> a_map;     a_map['func1'] = MultiFunc::Create(&func1);     (*a_map['func1'])('Hello');     a_map['func2'] = MultiFunc::Create(&func2);     (*a_map['func2'])(3);      // Remember to delete the MultiFunc object, or use smart pointers. } 

    This outputs:

     Hello 3 

    Unfortunately, you can’t make templated virtual functions or you easily generalize this all.

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

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I feel that this is one of the few scenarios… May 11, 2026 at 3:00 pm
  • added an answer If your tree widget is called treeWidget, you should be… May 11, 2026 at 3:00 pm
  • added an answer Sometimes I have exactly one assert per test case, but… May 11, 2026 at 3:00 pm

Related Questions

I'm not sure if this is a strange thing to do or not, or
I am looking for a way to get a function in excel to retrieve
I have a list of objects and I would like to access the objects
What kind of open-source libraries are available to convert XML into a java value

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.