This code compiles and works correctly:
#include <ranges>
#include <iostream>
int main() {
const auto r = std::views::iota('a', static_cast<char>('g' + 1));
for(const auto& [start, end] : r | std::views::chunk(3u)) {
for(auto it = start; it != end; ++it) {
std::cout << *it << " ";
}
std::cout << "\n";
}
return 0;
}
Its output is:
a b c
d e f
g
If I change the definition of r
as follows:
const auto r = std::views::iota('a', 'g' + 1);
The code does not compile. GCC 13 emits the following error:
chunk.cpp:13:21: error: cannot decompose inaccessible member ‘std::ranges::take_view<std::ranges::subrange<std::ranges::iota_view<char, int>::_Iterator, std::ranges::iota_view<char, int>::_Sentinel, std::ranges::subrange_kind::unsized> >::_M_base’ of ‘const std::ranges::take_view<std::ranges::subrange<std::ranges::iota_view<char, int>::_Iterator, std::ranges::iota_view<char, int>::_Sentinel, std::ranges::subrange_kind::unsized> >’
13 | for(const auto& [start, end] : r | std::views::chunk(3u)) {
| ^~~~~~~~~~~~
In file included from chunk.cpp:1:
/usr/include/c++/13/ranges:2153:11: note: declared private here
2153 | _Vp _M_base = _Vp();
| ^~~~~~~
I think that 'g' + 1
is causing integer promotion and is making the first and second parameter of iota()
have different types. However, in other occasions, this seems fine. For example, this code works as expected:
#include <ranges>
#include <iostream>
int main() {
const auto r = std::views::iota('a', 'g' + 1);
for(const auto& val : r) {
std::cout << val << " ";
}
std::cout << "\n";
return 0;
}
What is happening here? And why does the error mentions (of all things) private members?