1

In this simple test case, how do I flatten it from vector<vector<T>> to vector<T>?

#include <iostream>
#include <vector>
#include <ranges>

int main() {
    std::vector v{1, 2, 3, 4, 5};

    for (const auto &v : v | std::views::transform([](int i) { return std::vector{i, i * 2}; }))
        std::cout << v << std::endl;

    return 0;
}

appending | std::views::join at, behind, or in front of, the transform results in the following error:

error: class template argument deduction failed:
 2700 |  return join_view{std::forward(__r)};

So, how am I supposed to do this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Foxie
  • 117
  • 1
  • 7
  • 1
    `join` works on a plain `vector>`. I'd say it's likely that it's a lifetime issue—you're creating temporary vectors from inside the transformation and the library wants full assurance they'll live long enough for the views to be valid. Someone more familiar with the library can give something more concrete. (However, consider where those vectors are going to live such that they last for more than one iteration. The views certainly don't store containers.) – chris Jul 29 '20 at 01:12

0 Answers0