Code compiles with g++14. I would like to know if there is a way to avoid calling ranges::to() so many times.
I was thinking maybe there is a way to use a tuple instead of a vector to accomodate such ranges of different types (if they hadn't been converted to string).
#include <ranges>
#include <vector>
#include <format>
#include <initializer_list>
#include <string>
using namespace std;
using namespace std::literals;
struct Point
{
int x, y;
};
struct Polyline
{
int from;
int to;
vector<Point> data;
};
int main()
{
const vector<Polyline> polylines = {
{.from=0, .to=1, .data={{.x=2,.y=2},{.x=4,.y=4}}},
{.from=1, .to=2, .data={{.x=20,.y=20},{.x=40,.y=40}}}
};
auto il = {
"["s,
polylines | views::transform([](auto arg){
auto [from, to, data]=arg;
auto il = {
"{"s,
R"("polyline":)"s,
"["s,
data | views::transform([](auto arg){auto [x, y]=arg; return format(R"({{"x":{}, "y":{}}})", x, y);})
| views::join_with(',')
| ranges::to<string>(),
"],"s,
format(R"("from":{},"to":{})", from, to),
"}"s
};
return il | views::join | ranges::to<string>();
}) | views::join_with(",\n"s) | ranges::to<string>(),
"]"s
};
string buffer = il | views::join_with('\n') | ranges::to<string>();
}