samedi 30 mars 2019

How do I view::concat 2 different range views?

I'm trying to view::concat 2 views. I don't understand when I can and can't do this, and why. Any help would be great. This question sounds similar, but doesn't address my problem.

I tried the following code

#include <iostream>
#include <range/v3/all.hpp>
using namespace ranges;

int main () {
  // 'string' of spaces   
  auto spaces = view::repeat_n(' ',4);  // 1
  // prints [ , , , ]
  std::cout << spaces  << std::endl;

  // 'string' of letters
  auto letters = view::iota('a', 'a' + 4); // 2
  // prints [a,b,c,d]   
  std::cout << letters  << std::endl;

  // 'string' from concat of letters and spaces   
  auto text = view::concat(letters,spaces); // 3
  // prints [a,b,c,d, , , , ]       
  std::cout << text  << std::endl;

  // 'vector<string>' repeat letters
  auto letter_lines = view::repeat_n(letters,3); // 4
  // prints [[a,b,c,d],[a,b,c,d],[a,b,c,d]]
  std::cout << letter_lines  << std::endl;

  // 'vector<string>' concat 2 repeated letter_lines
  auto multi_letter_lines = view::concat(letter_lines,letter_lines); // 5
  // prints [[a,b,c,d],[a,b,c,d],[a,b,c,d],[a,b,c,d],[a,b,c,d],[a,b,c,d]]
  std::cout << multi_letter_lines << std::endl;

  // 'vector<string>' from concat of letter_lines, and spaces_lines
  // this doesn't work (well it compiles)
  auto text_lines = view::concat(letter_lines,space_lines);
  // this doesn't compile
  std::cout << text_lines  << std::endl;   //  6 ERROR
  // I expected [[a,b,c,d],[a,b,c,d],[a,b,c,d],[ , , , ],[ , , , ],[ , , , ]]

  // This works
  auto flat_text_lines = view::concat(letter_lines | view::join,
                                      space_lines | view::join); // 7
  // prints [a,b,c,d,a,b,c,d,a,b,c,d, , , , , , , , , , , , ]
  std::cout << flat_text_lines << std::endl;
  // but the structure is lost; it's a 'string', not a 'vector<string>'
}

The cout after line 6 gives the error

note:   template argument deduction/substitution failed:
concat.cpp:21:19: note:   cannot convert ‘text_lines’ (type‘ranges::v3::concat_view<ranges::v3::repeat_n_view<ranges::v3::iota_view<char, int>>, ranges::v3::repeat_n_view<ranges::v3::repeat_n_view<char> > >’) to type ‘const ranges::v3::repeat_n_view<char>&’
      std::cout << text_lines  << std::endl;

If I understand the error, it's saying, concat<repeat_n<iota<char>>,repeat_n<repeat_n<char>>> can't be converted to repeat_n<char>. Ok, I actually want the concat to convert to something like repeat_n<repeat_n<char>>, so the error kind of makes sense.

But then I would expect the cout after line 3 to complain by saying something like concat<iota<char>,repeat_n<char>> can't be converted to repeat_n<char>.

Why does line 3 work; what type does it actually become? What should I do to get line 6 to work?

Aucun commentaire:

Enregistrer un commentaire