使用已弃用的绑定程序和 C++0x lambda
C++0x 已弃用旧的绑定器,例如 bind1st
和 bind2nd
,转而使用通用 std::bind
。 C++0x lambda 与 std::bind
很好地绑定,但它们不与经典的 bind1st 和 bind2nd 绑定,因为默认情况下 lambda 没有嵌套 typedef,例如 argument_type
、first_argument_type
、second_argument_type
和 result_type
。 所以我认为 std::function 可以作为将 lambda 绑定到旧绑定器的标准方法,因为它公开了必要的 typedef。
但是,在这种情况下使用 std::function 很难使用,因为它迫使您在实例化函数时拼写出函数类型。
auto bound =
std::bind1st(std::function<int (int, int)>([](int i, int j){ return i < j; }), 10); // hard to use
auto bound =
std::bind1st(std::make_function([](int i, int j){ return i < j; }), 10); // nice to have but does not compile.
我找不到 std::function 的方便对象生成器。像 std::make_fuction
这样的东西会很好。这样的事存在吗?如果没有,是否有其他更好的方法将 lamda 绑定到经典绑定器?
C++0x has deprecated the use of old binders such as bind1st
and bind2nd
in favor of generic std::bind
. C++0x lambdas bind nicely with std::bind
but they don't bind with classic bind1st and bind2nd because by default lambdas don't have nested typedefs such as argument_type
, first_argument_type
, second_argument_type
, and result_type
.
So I thought std::function
can serve as a standard way to bind lambdas to the old binders because it exposes the necessary typedefs.
However, using std::function
is hard to use in this context because it forces you to spell out the function-type while instantiating it.
auto bound =
std::bind1st(std::function<int (int, int)>([](int i, int j){ return i < j; }), 10); // hard to use
auto bound =
std::bind1st(std::make_function([](int i, int j){ return i < j; }), 10); // nice to have but does not compile.
I could not find a convenient object generator for std::function
. Something like std::make_fuction
would be nice to have. Does such a thing exist? If not, is there any other better way of binding lamdas to the classic binders?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从未尝试过做这样的事情,而且我没有时间提供完整的答案,但我想可以使用 Boost.FunctionTypes 来完成一些事情。
这是一个粗略的、不完整的、未经测试的草稿,可以给你一个想法:
Never tried to do such thing, and I don't have the time to provide a full answer, but I guess that something could be done with Boost.FunctionTypes.
Here's a rough, incomplete and untested draft to give you an idea:
我不知道为什么你仍然需要bind1st等。我唯一能想到的就是支持旧代码。实际上,
bind1st(f, a)
可以替换为[a](v){ return f(a, v); }
等,这是一个通用的解决方案。I don't know why you still need the bind1st etc. The only thing I could think of is to support old code. Actually,
bind1st(f, a)
can be replaced by[a](v){ return f(a, v); }
etc, and this is a universal solution.