将lambda存储到std ::功能的可能存储浪费

发布于 2025-01-20 12:40:08 字数 281 浏览 4 评论 0原文

带有空捕获列表的lambda表达式对象的大小为1

,但是如果将其存储到std:function中,它的大小将变为48(在我的平台上),

想象一下当您拥有一个存储数千个容器时函数的

内存使用情况将增加48倍的时间,如果将它们存储到std :: function

甚至捕获小对象的lambda对象(例如8尺度的指针)也比std ::函数

您是否有更好的主意来保存不必要的空间使用?

The size of a lambda expression object with an empty capture-list is 1

But if you store it into std:function, its size becomes 48 (on my platform)

Imagine when you have a container that stores thousands of functions

The memory usage will be 48-times bigger if you store them into std::function

Even a lambda object capturing a small object (like an 8-sized pointer) is much smaller than a std::function

Do you have any better idea to save that unnecessary space-usage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-01-27 12:40:08

这是您不需要知道功能类型的价格。所有std :: function&lt; void()&gt; s均可互换,无论它们来自哪个lambda。如果要在向量中存储很多相同类型的函数(带有不同的捕获),则可以使其成为函数,而不是lambda(以便它具有名称),并制作该类型<的向量 /em>。

示例:使用lambda:

std::vector<std::function<void()>> funcs;
for(int i = 0; i < 10000; i++)
    funcs.push_back([i]() {std::cout << i << std::endl;});
for(auto& func : funcs)
    func();

使用Foundor:

struct number_printing_function {
    int i;
    number_printing_function(int i) : i(i) {}
    void operator()() {
        std::cout << i << std::endl;
    }
};

std::vector<number_printing_function> funcs;
for(int i = 0; i < 10000; i++)
    funcs.push_back(number_printing_function(i));
    // or funcs.emplace_back(i);

for(auto& func : funcs)
    func();

IMO这有点没用,因为我们不妨存储一个INT的向量并停止假装它们是函数。当您拥有许多相同类型的函子时,您已经知道他们在做什么,所以就这样做。实际上,上面的代码只是下面的代码,但采用了额外的步骤:

std::vector<int> ints;
for(int i = 0; i < 10000; i++)
    ints.push_back(i);
for(auto& i : ints)
    std::cout << i << std::endl;

This is the price you pay for not needing to know the type of the function. All std::function<void()>s are interchangeable no matter which lambda they came from. If you want to store lots of the same type of function (with different captures) in a vector, you can make it a functor instead of a lambda (so that it has a name) and make a vector of that type.

Example: With lambda:

std::vector<std::function<void()>> funcs;
for(int i = 0; i < 10000; i++)
    funcs.push_back([i]() {std::cout << i << std::endl;});
for(auto& func : funcs)
    func();

With functor:

struct number_printing_function {
    int i;
    number_printing_function(int i) : i(i) {}
    void operator()() {
        std::cout << i << std::endl;
    }
};

std::vector<number_printing_function> funcs;
for(int i = 0; i < 10000; i++)
    funcs.push_back(number_printing_function(i));
    // or funcs.emplace_back(i);

for(auto& func : funcs)
    func();

IMO this is a bit useless, because we might as well store a vector of ints and stop pretending they are functions. When you have many functors of the same type you already know what they do, so just do it. Really, the code above is just the code below, but with extra steps:

std::vector<int> ints;
for(int i = 0; i < 10000; i++)
    ints.push_back(i);
for(auto& i : ints)
    std::cout << i << std::endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文