拉姆达常数?
c++0x 支持通过引用捕获值的 lambda:
[&] -> ret_t { return 0; }
捕获 const 引用有意义吗?
[const &] -> ret_t { return 0; }
更根本的是手头的问题; 有没有办法检测作为 std::function
传递的给定 lambda 是否没有副作用?
c++0x supports lambdas that capture values by reference:
[&] -> ret_t { return 0; }
Does it make sense to capture const references?
[const &] -> ret_t { return 0; }
More fundamentally to the question at hand; is there a way to detect if a given lambda being passed as a std::function<>
is free of side-effects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您甚至无法保证
std::function
中的内容是否是 lambda。你当然不能保证它没有副作用。如果您想确保您编写的 lambda 函数没有副作用(在可以确保这种情况的范围内),唯一的方法就是什么也不捕获:
[]
。You can't even guarantee that what's in a
std::function
is a lambda or not. You certainly can't guarantee that it has no side effects.If you want to ensure that the lambda function you write doesn't have side effects (to the extent that such things can be ensured), the only way to do that is to capture nothing:
[]
.