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
?
If this was doable in C++03, why would C++0x have introduced lambdas? There's a reason lambdas exist, and it's because binding and all the other C++03 solutions suck hideously.
Yes you can, but you'll have to implement one or more virtual methods declared in an interface.
Then you can define and use a local closure:
Credit to @Omnifarious for this improvement (
my_unary_functor
not needed):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 wantptr_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.