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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:00:48+00:00 2026-05-23T06:00:48+00:00

I want to save the binder of a function to a variable, to use

  • 0

I want to save the “binder” of a function to a variable, to use it repetitively in the following code by exploiting its operator overloading facilities. Here is the code that actually does what I want:

#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream>

class X 
{       
    int n; 
public: 
    X(int i):n(i){}
    int GetN(){return n;}  
};

int main()
{
    using namespace std;
    using namespace boost;

    X arr[] = {X(13),X(-13),X(42),X(13),X(-42)};
    vector<X> vec(arr,arr+sizeof(arr)/sizeof(X));

    _bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);

    cout << "With  n =13 : " 
         << count_if(vec.begin(),vec.end(),bindGetN == 13)
         << "\nWith |n|=13 : " 
         << count_if(vec.begin(),vec.end(),bindGetN == 13 || bindGetN == -13)
         << "\nWith |n|=42 : " 
         << count_if(vec.begin(),vec.end(),bindGetN == 42 || bindGetN == -42) 
         << "\n";

    return 0;                                                                    
} 

What bothers me is, of course, the line:

bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);

I’ve obtained the type just by deliberately making a type error and analysing the error message. That is certainly not a good way to go. Is there a way to obtain the type for the “bindGetN”? Or, maybe there are different ways to produce similar functionality?

Edit: I forgot to mention that the, so to say, “standard” suggestion to use function is not working in this case — because I’d like to have my operator overloading.

  • 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-23T06:00:49+00:00Added an answer on May 23, 2026 at 6:00 am

    The short answer is: you don’t need to know (implementation defined).
    It is a bind expression (std::tr1::is_bind_expression<T>::value yields true for the actual type).

    Look at

    1. std::tr1::function<>
    2. BOOST_AUTO()
    3. c++0x ‘auto’ keywords (Type Inference)
      • it’s close cousing decltype() can help you move further

    1.

    std::tr1::function<int> f; // can be assigned from a function pointer, a bind_expression, a function object etc
    
    int realfunc();
    int realfunc2(int a);
    
    f = &realfunc;
    int dummy;
    f = tr1::bind(&realfunc2, dummy);
    

    2.

    BOOST_AUTO() aims to support the semantics of c++0x auto without compiler c++0x support:

    BOOST_AUTO(f,boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
    

    3.

    Essentially the same but with compiler support:

    template <class T> struct DoWork { /* ... */ };
    
    auto f = boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
    
    DoWork<decltype(T)> work_on_it(f); // of course, using a factory would be _fine_
    

    Note that auto has probably been invented for this kind of situation: the actual type is a ‘you don’t want to know’ and may vary across compilers/platforms/libraries

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

Sidebar

Related Questions

I want to save all following Exceptions in a file. The reason why I
I want to save the date and time from PHP to SQL. Here is
I want to save my canvas into MySQL database (BLOB cell). I have function
I want to save data into a server. This is my simple php code:
I want to save some global vars to use in the website, like current
I want to save the objects I generated in a program. After restart the
I want to save and store simple mail objects via serializing, but I get
I want to save history of changes, so in @PostUpdate i want to create
I want to save my program's DataModel objects to a file, and be able
I want to save a local copy of xml being ouput by a certain

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.