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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:32:24+00:00 2026-06-13T11:32:24+00:00

How to implement a very simple edition of boost::bind, which does not bind arguments,

  • 0

How to implement a very simple edition of boost::bind, which does not bind arguments, but offer a way to call member function in c++ classes.

Here is my first try:

#include <iostream>

struct Foo {
    void x(int i) { std::cout << "Foo " << i << std::endl; }
};

struct Bar {
    void y(int i) { std::cout << "Bar " << i << std::endl; }
};

template<typename A1, typename I, typename M>
struct Binder {
    Binder(I i, M m) : i_(i), m_(m) { }
    void operator()(A1 a1) {
        (i_->*m_)(a1);
    }

    I i_;
    M m_;
};

template<typename A1, typename I, typename M>
Binder<A1, I, M> my_bind(I i, M m) {
    return Binder<A1, I, M>(i, m);
}

int main(int argc, const char *argv[])
{
    Foo foo;
    Bar bar;

    Binder<int, Foo*, void (Foo::*)(int)> b1 = my_bind<int>(&foo, &Foo::x);
    Binder<int, Bar*, void (Bar::*)(int)> b2 = my_bind<int>(&bar, &Bar::y);

    b1(1);
    b2(2);

    return 0;
}

The implementation above does work, and will print:

Foo 1
Bar 2

The problem is that the two invokes of my_bind returns objects of different types. How can I alter the program, such that my_bind will return a type which only depends on A1.

  • 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-13T11:32:25+00:00Added an answer on June 13, 2026 at 11:32 am

    The problem is that the two invokes of my_bind returns objects of different types. How can I alter the program, such that my_bind will return a type which only depends on A1.

    It is possible to do with type erasure.

    In short:

    1. Create Abstract class with interface you like to have. It may have some template parameters. (AbstractBinder in below code)
    2. Create Concrete class which implements this interface. Concrete class may have more template arguments than interface. (Binder class below)
    3. Create Holder class with template constructor – which creates Concrete class, but stores only pointer to it’s base Abstract class. So, Holder class has only template parameters required for Abstract interface, while its constructor has all the rest template parameters required for Concrete class. (BinderHolder class below)

    live demo

    Usage:

    int main()
    {
        Foo foo;
        Bar bar;
    
        BinderHolder<int> b1 = my_bind<int>(&foo, &Foo::x);
        BinderHolder<int> b2 = my_bind<int>(&bar, &Bar::y);
    
        b1(1);
        b2(2);
    }
    

    Full code:

    template<typename A1>
    struct AbstractBinder
    {
        virtual void call(A1 a1)=0;
        virtual AbstractBinder<A1> *clone()=0;
        virtual ~AbstractBinder(){}
    };
    
    template<typename A1, typename I, typename M>
    struct Binder : AbstractBinder<A1>
    {
        Binder(I i, M m) : i_(i), m_(m) { }
        void call(A1 a1)
        {
            (i_->*m_)(a1);
        }
        virtual AbstractBinder<A1> *clone()
        {
            return new Binder(*this);
        }
        I i_;
        M m_;
    };
    
    template<typename A1>
    class BinderHolder
    {
        AbstractBinder<A1> *ptr;
        BinderHolder &operator=(const BinderHolder&);
    public:
        template<typename I, typename M>
        BinderHolder(I i, M m)
            : ptr(new Binder<A1,I,M>(i,m))
        {
        }
        BinderHolder(const BinderHolder &rhs)
            : ptr(rhs.ptr->clone())
        {
        }
        ~BinderHolder()
        {
            delete ptr;
        }
        void operator()(A1 a1)
        {
            ptr->call(a1);
        }
    };
    
    template<typename A1, typename I, typename M>
    BinderHolder<A1> my_bind(I i, M m) {
        return BinderHolder<A1>(i, m);
    }
    
    #include <iostream>
    
    struct Foo {
        void x(int i) { std::cout << "Foo " << i << std::endl; }
    };
    
    struct Bar {
        void y(int i) { std::cout << "Bar " << i << std::endl; }
    };
    
    int main()
    {
        Foo foo;
        Bar bar;
    
        BinderHolder<int> b1 = my_bind<int>(&foo, &Foo::x);
        BinderHolder<int> b2 = my_bind<int>(&bar, &Bar::y);
    
        b1(1);
        b2(2);
    }
    

    P.S. If you are sure, that all your Concrete classes would have same size, then you can replace heap allocations with placement new inside to fixed size buffer, and add static_assert for safety.

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

Sidebar

Related Questions

I would like to implement a very simple way to store a variable containing
This is probably very simple but it's really confusing me. When I implement the
I need to implement a tree of decisions, very simple, not complicated in objective
I have a very simple issue, but I am not able to find the
I need to implement a very simple web-server-like app in Python which would perform
I am trying to implement a very simple example of FormsAuthentication. It is not
On my quest to implement a very simple 'drag' mechanism to my application (which
I am using a HttpListener to implement a very simple http server, which is
I'm trying to implement a very simple way to select a subsection of the
This is a very simple question, but embarrassingly enough I am not sure how

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.