Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilers disagree on accepting this view::keys code

Out of 3 compilers only gcc accepts this code. I guess clang problem is that it uses antique libstdc++, but MSVC and GCC should support ranges code.

What is the standard mandated behavior?

#include<array>
#include<algorithm>
#include<iostream>
#include<map>
#include<set>
#include<ranges>

constexpr std::array<std::pair<int, double>,2> arr{std::pair{1,1.1},std::pair{2,2.2}};

int main(){
    auto it = std::ranges::find(arr | std::views::keys , 2);    
    std::cout << *it;
}

https://godbolt.org/z/hnnb38cPM

like image 710
NoSenseEtAl Avatar asked Aug 28 '26 08:08

NoSenseEtAl


1 Answers

This needs P2017R1 (conditionally borrowed ranges) to work; without that, arr | std::views::keys is not a borrowed_range, and so ranges::find returns dangling.

Presumably the MSVC version on godbolt doesn't have that paper implemented.

like image 151
T.C. Avatar answered Aug 30 '26 23:08

T.C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!