0

I am trying to get into ranges::views.

The following demo program does not compile:

#include <iostream>
#include <sstream>
#include <string>
#include <iterator>
#include <ranges>
#include <algorithm>

namespace rng = std::ranges;

struct CompleteLine {    // Line Proxy for the input Iterator
    friend std::istream& operator>>(std::istream& is, CompleteLine& cl) { std::getline(is, cl.completeLine); return is; }
    operator std::string() const { return completeLine; }  // cast operator
    std::string completeLine{};
};

std::istringstream iss{ R"(1 Line AAA
2 Line BBB
3 Line CCC)" };

int main() {
    for (const std::string& line : std::ranges::views::istream<CompleteLine>(iss)
        | std::views::transform([](std::string line) {
            std::ranges::transform(line, line.begin(),
                [](auto c) { return std::tolower(c); });
            return line;
            })) {
        std::cout << line << '\n';
    }
}

Error messages:

s\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(21,56): error C2039: 'istream': is not a member of 'std::ranges::views'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\ranges(4051): message : see declaration of 'std::ranges::views'
1>C:\Users\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(21,64): error C2275: 'CompleteLine': illegal use of this type as an expression
1>C:\Users\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(10): message : see declaration of 'CompleteLine'
1>C:\Users\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(21,5): error C2530: 'line': references must be initialized
1>C:\Users\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(21,34): error C2143: syntax error: missing ';' before ':'
1>C:\Users\Armin\source\repos\Stackoverflow\Stackoverflow\stackoverflow.cpp(26,15): error C2143: syntax error: missing ';' before ')'
1>Done building project "Stackoverflow.vcxproj" -- FAILED.

I am using "Microsoft Visual Studio Community 2019, Version 16.11.9" with "Preview - Features from the Latest C++ Working Draft (/std:c++latest)"


Can somebody tell me the correct syntax for the above program to work please?

A M
  • 14,694
  • 5
  • 19
  • 44
  • 1
    "*Microsoft Visual Studio Community 2019*" This almost certainly does not support C++20. Or at least, not the ranges portion of it. Upgrade to newer versions of VC++ 2022. – Nicol Bolas Dec 19 '22 at 06:25
  • 1
    There should be another tab with the full and complete build output, which can be copy-pasted as text. Please use it instead of the error list. – Some programmer dude Dec 19 '22 at 06:26
  • @NicolBolas. It does support ranges and I'm am using it. I cannot upgrade. I am on Windows 7 in a sandbox. – A M Dec 19 '22 at 06:34
  • 2
    If version information at compiler explorer can be trusted, then this should compile with your MSVC version, but not 16.10 before it, see https://godbolt.org/z/MnrPv1593. – user17732522 Dec 19 '22 at 06:42
  • "_Can somebody tell me the correct syntax for the above program to work please?_": The syntax is correct. If it doesn't compile, then your compiler doesn't fully support C++20 ranges yet. – user17732522 Dec 19 '22 at 06:46
  • @AM `I am on Windows 7`. Even *Windows 7* isn't supported anymore....well, for two more weeks as of this comment. If you can't upgrade, consider finding another job or college or ....well if you're in high school you're SOL. – Casey Dec 19 '22 at 06:49
  • @user17732522 Thank you. So, it is a compiler issue. What a pity . . . – A M Dec 19 '22 at 08:50

1 Answers1

3

P2432 (C++20 DR) allows us to construct a basic_istream_view object through customization point object views::istream.

But before that, we need to invoke the free function istream_view(...) to get a basic_istream_view object.

I suspect that the version of MSVC you are using has not implemented P2432, so the workaround is just to replace views::istream with ranges::istream_view

for (const std::string& line : std::ranges::istream_view<CompleteLine>(is)
                             | std::views::transform([](std::string line) {
   // ...
}
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • Astonishingly this works. So C++20 is only partly integrated in my VS version. This is wasting time of everybody . . . – A M Dec 19 '22 at 08:53