[libcxx-commits] [libcxx] [libc++] `std::views::split`: fix handling of empty ranges (LWG4017) (PR #87916)

Jan Kokemüller via libcxx-commits libcxx-commits at lists.llvm.org
Sun Apr 7 04:16:39 PDT 2024


jiixyj wrote:

I stumbled upon this issue when I tried to use `std::views::split` on a (possible empty) string. This is quite a nasty footgun. I had to work around in my code like this:

```c++
    auto format = [&](auto&& rng) {
        for (const auto& line : rng) {
            out = std::format_to(out, "element: {}\n", std::string_view{line});
        }
    };

    if (str.empty()) {
        format(std::views::single(str));
    }
    else {
        format(str | std::views::split('\n'));
    }
```

https://github.com/llvm/llvm-project/pull/87916


More information about the libcxx-commits mailing list