ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.05k stars 437 forks source link

view chunk can't compile? #1797

Closed dangdkhanh closed 8 months ago

dangdkhanh commented 8 months ago

Hi, I'm using the following code but it has not compiled.

std::wstring s{L"abcdef"};
auto x= s | ranges::views::chunk(2) | ranges::views::intersperse(L"-");

Is there a way to make it work without converting back to vector? Thanks you!

brevzin commented 8 months ago

intersperse takes a range and a value type of that range. L"-" is not convertible to the value type of s | chunk(2) (which in this case is a ranges::take_view<...>), so this can't really work.

One way to fix this would be to first produce a range of wstring and then interspersing wstring(L"-"). The string version of that looks like this:

fmt::print("{}\n",
    s                                                       // range of char
    | ranges::views::chunk(2)                               // range of take_view<...>
    | ranges::views::transform(ranges::to<std::string>())   // range of std::string
    | ranges::views::intersperse(std::string("-"))          // range of std::string
    );

Since s is contiguous, you could also get away with producing a range of string_view and interspersing a string_view.