一、背景

Out-of-line Lambdas翻译过来就是“线外Lambda函数”或“离线Lambda函数”。Lambda 是使代码更具表现力的好工具,Out-of-line Lambdas是指在C++编程语言中,将Lambda函数的定义和实现分离的一种技术。Lambda函数是一种能够在代码中方便地定义匿名函数的特性,它可以在需要函数对象的地方直接使用,并且可以捕获周围作用域的变量。

通常情况下,Lambda函数是内联定义的,也就是在使用它的地方直接定义和使用,这样可以方便地将函数逻辑放在需要的地方,提高代码的可读性和可维护性。但是,有时候Lambda函数的实现逻辑较为复杂,或者需要在多个地方重复使用,这时就可以使用Out-of-line Lambdas来将Lambda函数的定义和实现分开。

使用Out-of-line Lambdas,可以将Lambda函数的定义放在一个地方,而将实现放在另一个地方,通过函数指针或函数对象的方式进行调用。这样做的好处是可以将复杂的函数逻辑从主要代码中分离出来,使主要代码更加简洁和易读。同时,Out-of-line Lambdas也可以在多个地方重复使用,提高代码的重用性。

如下代码:

展开

代码语言:C++

自动换行

AI代码解释


auto const product = getProduct(); std::vector<Box> goodBoxes; std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), [product](Box const& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; });

不希望在调用代码的中间看到这种细节。这就提出了一个问题:什么时候应该使用动态临时 lambda,以及何时应该创建Out-of-line Lambda函数来减轻调用点的负担。

代码语言:C++

自动换行

AI代码解释


auto const product = getProduct(); std::vector<Box> goodBoxes; std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), resists(product));

这个解决方案看起来更好,因为 lambda 的主体处于比周围代码更低的抽象级别。

不过,这并不意味着应该避免使用 lambda。 resists可以使用 Out-of-line lambda 函数实现:

展开

代码语言:C++

自动换行

AI代码解释


auto resists(Product const& product) { return [product](Box const& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; }; }

如果以前没有见过这种技术,请花点时间阅读上面的代码:它是一个函数(resist),它获取上下文(product)并返回一个捕获product的函数(未命名的lambda)。

返回类型是 lambda 的类型,由于它是由编译器确定的,并且我们程序员不知道,因此这里使用一个方便的auto作为函数的返回类型。

但是上面的代码(至少)有一个问题,接着往下看。

二、lambda 的捕获

上面代码中的一个问题是 lambda 通过复制捕获。但没有必要在这里复制,这个lambda在语句末尾被std::copy_if破坏,并且product在此期间保持活动状态。lambda也可以通过引用来获取product

展开

代码语言:C++

自动换行

AI代码解释


auto resists(Product const& product) { return [&product](Box const& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; }; }

这等同于通过复制捕获的先前版本,只是此代码不创建副本。

这看起来都很好,只是如果稍微改变一下调用的地方,这段代码就会中断。调用点如下所示:

代码语言:C++

自动换行

AI代码解释


auto const product = getProduct(); std::vector<Box> goodBoxes; std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), resists(product));

如果给lambda起一个名字,同时去掉product中介对象,会怎么样?

代码语言:C++

自动换行

AI代码解释


std::vector<Box> goodBoxes; auto const isAGoodBox = resists(getProduct()); std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), isAGoodBox);

结果是,变成了未定义的行为。事实上,getProduct返回的Prouct现在是一个临时对象,在其语句结束时被销毁。当std::copy_if调用isGoodBox时,它会调用这个已经销毁的product

reslists中通过引用捕获使代码变得脆弱。

三、可能出现的警告

在大多数情况下,这段代码都是在没有任何警告的情况下编译的。编译器发出警告的唯一情况是:

  • 使用gcc;

  • 在优化水平为-O1的情况下;

  • 并且当使用对构造函数的直接调用构建临时对象时(Product{1.2})。

代码语言:C++

自动换行

AI代码解释


auto const isAGoodBox = resists(Product{1.2}); std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), isAGoodBox);

在这个示例中,警告是这样的:

代码语言:C++

自动换行

AI代码解释


warning: '<anonymous>.Product::density_' is used uninitialized in this function [-Wuninitialized] double getDensity() const { return density_; }

但在其他配置中(-O0-O2-O3,使用中介函数getProduct(),或使用clang编译)都没有产生警告。

四、lambda的广义捕获

可以使用广义lambda捕获将临时Product移动到我们的lambda中。C++14为lambdas带来了一个新特性:广义lambda捕获。它允许在lambda的捕获中执行一些自定义代码:

代码语言:C++

自动换行

AI代码解释


[context = f()](MyType const& myParameter){ /* body of the lambda */ }

让我们利用广义lambda捕获来移动临时对象:

展开

代码语言:C++

自动换行

AI代码解释


auto resists(Product&& product) { return [product = std::move(product)](const Box& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; }; }

通过对代码的这种修改,在临时product(从中移出)被销毁后,lambda将使用自己的prduct继续其生命周期。不再有未定义的行为。

现在,不能再使用之前的版本了:

代码语言:C++

自动换行

AI代码解释


auto const product = getProduct(); std::vector<Box> goodBoxes; std::copy_if(boxes.begin(), boxes.end(), std::back_inserter(goodBoxes), resists(product));

事实上,product在这里是一个左值,因此不能绑定到右值引用。为了强调这一点,编译器毫不客气地拒绝了这段代码:

代码语言:C++

自动换行

AI代码解释


error: cannot bind rvalue reference of type 'Product&&' to lvalue of type 'const Product'

五、为每种情况进行重载

一种解决方案是对resist进行两次重载:一次采用左值引用,另一次采用右值引用。

展开

代码语言:C++

自动换行

AI代码解释


auto resists(Product const& product) { return [&product](const Box& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; }; } auto resists(Product&& product) { return [product = std::move(product)](const Box& box) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; }; }

这会造成代码重复,这是我们应该避免的技术代码重复的情况之一。解决此问题的一种方法是将业务代码分解为由其他两个调用的第三个函数:

展开

代码语言:C++

自动换行

AI代码解释


bool resists(Box const& box, Product const& product) { const double volume = box.getVolume(); const double weight = volume * product.getDensity(); const double sidesSurface = box.getSidesSurface(); const double pressure = weight / sidesSurface; const double maxPressure = box.getMaterial().getMaxPressure(); return pressure <= maxPressure; } auto resists(Product const& product) { return [&product](const Box& box) { return resists(box, product); }; } auto resists(Product&& product) { return [product = std::move(product)](const Box& box) { return resists(box, product); }; }

通用解决方案:该解决方案的优点是,它通过隐藏较低级别的详细信息,允许在调用站点上使用表达型代码,并且它对左值和右值都能正确工作。一个缺点是它创建了lambda的多个重载的样板。

六、总结

如果Out-of-line Lambdas利大于弊,减轻缺点会很有趣。一种方法是创建一个通用组件来封装多个重载的机制。使用这个通用组件,而不是每次都编写样板文件。


 

更多推荐