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
  • 1 View
  • 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

Related Questions

I was wondering if there was a way to accomplish this, or if there
I was wondering if there is a way to simplify this down from two
I am wondering if there is a way to do this using Core Animation.
I am wondering if there is a way to handle this more elegantly. After
I am wondering if there is a better way to write this JavaScript (Jquery)
I'm wondering if there is a more elegant way to do this: $foo =
I'm wondering if there's a shorter way to write this: var x = 1;
If I have this line and I'm wondering if there's better way to do
I was wondering if there is a way, using PHP, to change this date
Cascading deletes aren't setup on this particular database. Just wondering if there's a way

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.