Syntax checks for function composition operator

Answered

This demo code from the bottom of the cppreference.com ranges library page compiles and runs in the IDE fine (Fedora 33 system, using the stock gcc 10.3.1).   

#include <ranges>
#include <iostream>
 
int main()
{
    auto const ints = {0,1,2,3,4,5};
    auto even = [](int i) { return 0 == i % 2; };
    auto square = [](int i) { return i * i; };
 
    // "pipe" syntax of composing the views:
    for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
        std::cout << i << ' ';
    }
 
    std::cout << '\n';
 
    // a traditional "functional" composing syntax:
    for (int i : std::views::transform(std::views::filter(ints, even), square)) {
        std::cout << i << ' ';
    }
}

However, in the editor I get 3 errors, two for the function composition operator (`|`) in the first `for` clause:

In template: no matching function for call to '__ranges_begin'

Invalid operands to binary expression

And one for the colon in the second loop:

Invalid range expression of type 'std::ranges::views::_adapter[...]': no viable 'begin' function available.

I presume this is from `clangd`, which is the built in v. 14.0.0., which seems strange since as far as I can tell clang should support this -- although by that chart I would have thought the system clang (v. 11.0.0) should have too and it does not (admittedly I am not a clang user nor have I been able find a proposal number for this idiom).   

Disabling Languages & Frameworks -> C/C++ -> Clangd -> Show errors and warnings from clangd removes the errors but adds another, 'too many arguments, expected 1' for the tranform(filter) call in the second for clause, and obviously disabling compiler checks wholesale like this isn't desirable.

I'm using `CMAKE_CXX_STANDARD 20` although I don't think it applies here (again, actually building and running within the IDE is fine).

Is there anything I can do but wait for whatever component it is to be updated?

1 comment

Please sign in to leave a comment.