What would be the most concise and/or idiomatic way to create a std::string from a transformed view (of another string)?
The best I could come up is to use std::ranges::copy with the view and inserter as arguments, and even that is only accepted by gcc (10.2 & trunk) but not clang 11.x.
For most ranges algorithms, I thought the intent was that the operator| can be used whenever the first argument to a function is a range, so I wonder why the 2nd and IMHO cleaner variant doesn't compile (the error message is that no matching copy was found), and is the intent to maybe eventually have it compile, or what?
I tried with both gcc 10.2 and gcc (11) trunk. Clang doesn't compile the first form either :(
#include <algorithm>
#include <cctype>
#include <iterator>
#include <ranges>
#include <string>
int main() {
std::string original = "foo";
std::string upper;
// This compiles on gcc 10.2/11-trunk only
std::ranges::copy(
original
| std::ranges::views::transform([](char c){return std::toupper(c);}),
std::back_inserter(upper)
);
// The below doesn't compile period
original
| std::ranges::views::transform([](char c){return std::toupper(c);})
| std::ranges::copy(std::back_inserter(upper));
}