gpt4 book ai didi

c++ - 在 lambda 中延迟初始化和缓存内部值的干净方法

转载 作者:可可西里 更新时间:2023-11-01 17:53:07 26 4
gpt4 key购买 nike

先用天真的方法让代码自己说话:

int heavy_calc() // needed to be called once
{
// sleep(7500000 years)
return 42;
}

int main()
{
auto foo = [] {
// And cached for lambda return value
static int cache = heavy_calc();
return cache;
};
return foo() + foo();
}

我想在第一次调用时计算 lambda 内部缓存值。一种天真的方法是使用 static 缓存,但它 increases binary size and refuses to be be inlined .

我想出了在捕获列表中创建缓存并将lambda标记为可变,什么inlines without problems ,但要求缓存以默认值开始,这可能会破坏类不变性。


auto foo = [cache=0] () mutable {
// And cached for lambda return value
if(!cache)
cache = heavy_calc();
return cache;
};

我的第三种方法使用 boost::optional in mutable lambda

auto foo = [cache=std::optional<int>{}] () mutable {
// And cached for lambda return value
if(!cache)
cache = heavy_calc();
return *cache;
};

它工作正常,但在我看来是一种捕获列表 + mutable 关键字 hack。此外,mutable 会影响所有捕获的参数,因此在实际使用中会降低 lambda 的安全性。

也许对此有更好/更干净的解决方案?或者只是不同的方法最终产生了完全相同的效果。

编辑,一些背景:选择 Lambda 方法是因为我正在修改一些回调 lambda,目前用作:[this, param]{this->onEvent(heavy_calc(param));}我想减少 heavy_calc 调用而不预先评估它(仅在第一次调用时)

最佳答案

老实说,我看不出有什么理由在这里使用 lambda。您可以编写一个常规的可重用类来缓存计算值。如果您坚持使用 lambda,那么您可以将值计算移至参数,这样就无需进行任何操作 mutable:

int heavy_calc() // needed to be called once
{
// sleep(7500000 years)
return 42;
}

int main()
{
auto foo
{
[cache = heavy_calc()](void)
{
return cache;
}
};
return foo() + foo();
}

online compiler

通过一些模板,可以编写一个惰性评估和缓存任意计算结果的类:

#include <boost/optional.hpp>
#include <utility>

template<typename x_Action> class
t_LazyCached final
{
private: x_Action m_action;
private: ::boost::optional<decltype(::std::declval<x_Action>()())> m_cache;

public: template<typename xx_Action> explicit
t_LazyCached(xx_Action && action): m_action{::std::forward<xx_Action>(action)}, m_cache{} {}

public: auto const &
operator ()(void)
{
if(not m_cache)
{
m_cache = m_action();
}
return m_cache.value();
}
};

template<typename x_Action> auto
Make_LazyCached(x_Action && action)
{
return t_LazyCached<x_Action>{::std::forward<x_Action>(action)};
}

class t_Obj
{
public: int heavy_calc(int param) // needed to be called once
{
// sleep(7500000 years)
return 42 + param;
}
};

int main()
{
t_Obj obj{};
int param{3};
auto foo{Make_LazyCached([&](void){ return obj.heavy_calc(param); })};
return foo() + foo();
}

online compiler

关于c++ - 在 lambda 中延迟初始化和缓存内部值的干净方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54251340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com