c++ - boost::lambda::var 嵌套在 boost::bind 中不等同于 boost::lambda::var 本身

标签 c++ boost lambda c++03

我在识别一些简单的 boost::lambda 使用问题时遇到了一些麻烦。我可以像这样制作一个简单的 lambda 函数:

int i = 0;
boost::lambda::var(i) = boost::lambda::_3; // Set 'i' to the 3rd parameter.

但是一旦我将 lambda 函数包装在一个绑定(bind)中:

int i = 0;
boost::bind(boost::lambda::var(i) = boost::lambda::_3);  // Set 'i' to the 3rd parameter.

它变得不可用:

(boost::lambda::var(i) = boost::lambda::_3)(0,1,2); // Compiles & behaves as expected. i == 2
boost::bind(boost::lambda::var(i) = boost::lambda::_3)(0, 1, 2); // Compile error

boost::lambda::var 确实产生了一个可绑定(bind)函数吗?我是否以某种方式捏造了语法?它总是倾向于简单的东西,您可以发出的任何光都会受到赞赏:)

(使用 MSVC2008 & boost v1.50 编译)

最佳答案

我认为你需要防止替换

在这种情况下,boost::lambda::protect 似乎是有序的

此外,检查 Boost Bind 与 Boost Lambda 的关系:

http://www.boost.org/doc/libs/1_55_0/doc/html/lambda/s08.html#idp153645176

The Boost Bind [bind] library has partially overlapping functionality with the BLL. Basically, the Boost Bind library (BB in the sequel) implements the bind expression part of BLL. There are, however, some semantical differerences.

The BLL and BB evolved separately, and have different implementations. This means that the bind expressions from the BB cannot be used within bind expressions, or within other type of lambda expressions, of the BLL. The same holds for using BLL bind expressions in the BB. The libraries can coexist, however, as the names of the BB library are in boost namespace, whereas the BLL names are in boost::lambda namespace.

以及该页面上的更多信息

关于c++ - boost::lambda::var 嵌套在 boost::bind 中不等同于 boost::lambda::var 本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946834/

相关文章:

C++ 可执行文件在运行时找不到库,即使它位于/usr/lib(在 Linux 上)

c++ - boost :为什么 write_json 改变内容

python - 变量在 lambda 函数内如何工作?

node.js - 使用 nodeJS 调用 awscli

c++ - C++14 中的模板化函数参数

c++ - 构造函数不能是虚拟的,为什么?不是骗子

c++ - 为什么所有像素值的锁定位都返回 -842150451?

c++ - 期望在模拟对象上调用某些方法。它会破坏封装吗?

c++ - Boost::geometry 在点和多边形上使用方法内遇到问题?

c++ - 插件中的 qt 信号/插槽