是否可能创建函数本地闭包前C ++ 11? [英] Is it possible to create function-local closures pre-C++11?
问题描述
使用C ++ 11,我们可以获得lambdas,并且可以在实际需要的地方创建函数/函子/闭包,而不是它们真正属于的地方。
在C ++ 98/03中,一个很好的函数局部函数/闭包的方法如下:
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);
显然的问题是,你不能保存任何状态,因为本地结构/
我接下来想解决这个问题是使用 std :: bind1st
和 std的混合: :mem_fun
和非静态方法&变量,但不幸的是 std :: mem_fun
不知何故阻塞了 std :: mem_fn(& X :: functor)
这也可能是因为本地struct /类不能在模板中使用:
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));
在VC9& VC10(具有 / Za
,禁用的语言扩展),具有以下错误
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 &'
或在gcc 4.3.4下出现此错误
Or under gcc 4.3.4 with this error
error: no matching function for call to ‘mem_fun(void (main()::X::*)(int&))’
有趣的是,VC9 / VC10还是扼杀了上面的例子,即使语言扩展启用:
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
那么,标题中标注的功能是什么?或者在最后一个例子中,我如何使用 std :: bind1st
或 std :: mem_fun
时犯错误?
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
?
推荐答案
bind1st
仅适用于二进制函数,限制。 mem_fn
仅适用于非静态成员函数;对于你的应用程序,你会想要 ptr_fun
。
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
.
真正的C ++ 03最好的工作是Boost Bind,或者我将在这里演示 tr1 :: bind
这在我看来更便携。
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';
}
这篇关于是否可能创建函数本地闭包前C ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!