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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:24:12+00:00 2026-05-31T07:24:12+00:00

I would know if there is a way to pass arguments using std::mem_fun ?

  • 0

I would know if there is a way to pass arguments using std::mem_fun ?
I want to precise that I can have as much arguments as possible and a lot of member functions.
The problem is that I’m on an old standard and I’m looking for a full stl way so boost isn’t allowed as answer even if I know I could do it easily =/

Here is a little illustration of how I wanna use it :

#include <list>
#include <algorithm>

// Class declaration
//
struct Interface {
   virtual void run() = 0;
   virtual void do_something(int) = 0;
   virtual void do_func(int, int) = 0;
};

struct A : public Interface {
   void run() { cout << "Class A : run" << endl; }
   void do_something(int foo) { cout << "Class A : " << foo << endl; }
   void do_func(int foo, int bar) { cout << "Class A : " << foo << " " << bar << endl; }
};

struct B : public Interface {
   void run() { cout << "Class B : run" << endl; }
   void do_something(int foo) { cout << "Class B : " << foo << endl; }
   void do_func(int foo, int bar) { cout << "Class B : " << foo << " " << bar << endl; }
};

// Main
//
int main() {
   // Create A and B
   A a;
   B b;

   // Insert it inside a list
   std::list<Interface *> list;
   list.push_back(&a);
   list.push_back(&b);

   // This works
   std::for_each(list.begin(), list.end(), std::mem_fun(&Interface::run));

   // But how to give arguments for those member funcs ?
   std::for_each(list.begin(), list.end(), std::mem_fun(&Interface::do_something));
   std::for_each(list.begin(), list.end(), std::mem_fun(&Interface::do_func));
   return 0;
}
  • 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-31T07:24:13+00:00Added an answer on May 31, 2026 at 7:24 am

    Use std::bind via std::bind1st and std::bind2nd

    std::for_each(list.begin(), list.end(),
                  std::bind2nd(std::mem_fun(&Interface::do_something),1) // because 1st is this
                 );
    

    Unfortunately, the standard does not help for the two arguments version and you need to write your own:

    struct MyFunctor
    {
        void (Interface::*func)(int,int);
        int         a;
        int         b;
    
        MyFunctor(void (Interface::*f)(int,int), int a, int b): func(f), a(a), b(b) {}
    
        void operator()(Interface* i){ (i->*func)(a,b);}
    };
    
    std::for_each(list.begin(), list.end(),
                  MyFunctor(&Interface::do_func, 1, 2)
                 );
    

    Lambda version

    The original answer was good back in 2012 when Lambda’s had just been added to the standard and few compilers were C++11 compliant yet. Now 8 years later most compilers are C++11 compliant and we can use this to make these things much simpler.

    // Binding 1 parameter
    std::for_each(list.begin(), list.end(),
                  [](auto act){act->do_something(1);})
    
    // Binding 2 parameters
    std::for_each(list.begin(), list.end(),
                  [](auto act){act->do_func(1, 2);})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there is any way I can apply 100%
I would like to know if there is any way that I could build
I would like to know if there is any way I can access an
I would like to know if there is any way to add custom behaviour
I would like to know if there is some way to share a variable
I would like to know if there is a way to disable automatic loading
I would like to know if there is a way to keep the session
I would like to know if there is any way to return an char
I would like to know if there is a way to take create a
I would like to know if there is a way to test whenever thread

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.