为什么在std :: string` std ::功能上``std :: subrange'''std :: functional` lambda存储在``sTD :: vector'的lambda''中的行为呢?

发布于 2025-02-10 23:13:32 字数 1316 浏览 2 评论 0原文

考虑以下代码,其中包含一个模板std :: vector std :: function对象的类型> const& std :: range 和std :: range :: iterator。导致位置 std :: Ranges :: iterator_t传递到std :: functional lambda被无效的原因,以便在上, g ++clang ++std :: cout迭代通过std :: string的结尾?

#include <ranges>
#include <string>
#include <iostream>
#include <vector>
#include <functional>

int main(const int, const char * []) {
    auto consume = [](const auto & source, auto position) {
        auto consumption = std::ranges::subrange(position, std::end(source));
        for (auto res : consumption) {
            std::cout << res;
        }
    };

    std::vector<std::function<void(
            std::string,
            std::string::const_iterator)>> lambdas{consume};
    const auto range = std::string("hello");
    lambdas.back()(range, std::begin(range));
}

我不确定最小可行的例子 (更好的Asan'd版本由@marek R提供),但我已经尽力修剪了。

Consider the following code, which contains a templated std::vector type of std::function objects which consume a const & std::range and std::range::iterator. What causes the position std::ranges::iterator_t passed into the std::functional lambda to be invalidated, such that on g++ and clang++, the std::cout iteration goes passed the end of the std::string?

#include <ranges>
#include <string>
#include <iostream>
#include <vector>
#include <functional>

int main(const int, const char * []) {
    auto consume = [](const auto & source, auto position) {
        auto consumption = std::ranges::subrange(position, std::end(source));
        for (auto res : consumption) {
            std::cout << res;
        }
    };

    std::vector<std::function<void(
            std::string,
            std::string::const_iterator)>> lambdas{consume};
    const auto range = std::string("hello");
    lambdas.back()(range, std::begin(range));
}

I am uncertain what else is unnecessary for the minimal viable example (Better ASAN'd version courtesy of @Marek R), but I've done my best to trim.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文