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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:01:53+00:00 2026-05-21T16:01:53+00:00

With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we

  • 0

With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we actually need them, not somewhere where they don’t really belong.
In C++98/03, a nice way to make function-local functors/closures would’ve been the following:

struct{
  void operator()(int& item){ ++item; }
}foo_functor;
some_templated_func(some_args, foo_functor);

Sadly, you can’t use local types for templates (Visual Studio allows this with language extensions enabled). My train of though then went the following way:

struct X{
  static void functor(int& item){ ++item; }
};
some_templated_func(some_args, &X::functor);

The obvious problem being, that you can’t save any state, since local structs/classes can’t have static members.
My next thought to solving that problem was using a mix of std::bind1st and std::mem_fun and non-static methods & variables, but unfortunately std::mem_fun somehow chokes with std::mem_fn(&X::functor), which again might be because local struct/classes can’t be used in templates:

// wanted, not working solution
struct X{
  int n_;
  X(int n) : n_(n) {}
  void functor(int& item) const { item += n_; }
};
X x(5);
some_templated_func(some_args,std::bind1st(std::mem_fun(&X::functor),&x));

Fails under VC9 & VC10 (with /Za, disabled language extensions) with the following error

error C2893: Failed to specialize function template 'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(_Result (_Ty::* )(_Arg) const)'
With the following template arguments:
'void'
'main::X'
'int &'

Or under gcc 4.3.4 with this error

error: no matching function for call to ‘mem_fun(void (main()::X::*)(int&))’

Funnily enough, VC9 / VC10 still chokes on the above example, even with language extensions enables:

error C2535: 'void std::binder1st<_Fn2>::operator ()(int &) const' : member function already defined or declared

So, is the functionality stated in the title somehow, anyhow achievable? Or am I making a mistake in the last example in how I use std::bind1st or std::mem_fun?

  • 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-21T16:01:54+00:00Added an answer on May 21, 2026 at 4:01 pm

    bind1st only works for binary functions, and in general it’s very restricted. mem_fn works with non-static member functions only; for your application you would want ptr_fun.

    Really the best tool for the job in C++03 is Boost Bind, or I’ll demonstrate here with tr1::bind which is (in my opinion) more portable.

    #include <tr1/functional>
    #include <iostream>
    #include <algorithm>
    
    using namespace std::tr1::placeholders;
    
    int nums[] = { 1, 2, 4, 5, 6, 8 };
    
    int main() {
        struct is_multiple {
            static bool fn( int mod, int num ) { return num % mod == 0; }
        };
    
        int *n = std::find_if( nums, nums + sizeof nums/sizeof*nums,
                            std::tr1::bind( is_multiple::fn, 3, _1 ) );
    
        std::cout << n - nums << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im pretty confused about lambdas and actually im not even sure i need them
When creating a lambda expression by hand I get a 'Parameter not in scope'
Strange one that I don't still get, is this: Say, try { stateClient.Socket.BeginSend(messagePrefixed, 0,
I'd rather not use Linq if I don't have to (I remove references to
I have some Python code that's dependent upon passing around some lambdas, and they
I'm just not understanding on how to create a lambda expression instead of using
I am trying to create lambdas inside a loop that iterates over a list
I'm not familiar with delegates and lambdas but at this moment, I have to
I could not get the following test code work in XCode 4.5.2: #include<algorithm> #include<vector>
I want to get lambda function that will query for items which were submitted

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.