Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should std::get<T&>(tuple) work for zip_view's output?

Tags:

c++

std

c++20

c++23

In short: std::get<T&>('tuple-like') does not work when using ranges library in MSVC, and I wonder what is expected according to the C++ standard.

I cannot get the code below to compile correctly on MSVC 19.40. It does work with both gcc and clang, though, and I am yet to find information that speaks in favour of MSVC's behaviour.

sample code

#include <tuple>
#include <ranges>
#include <algorithm>

int main() {
    using namespace std::views;

    char foo{};

    std::ranges::for_each(zip(iota(0, 2) | transform([&](int i) -> char& { return foo; })),
                          [] (auto t) { auto bar = std::get<char&>(t); });

    return 0;
}

error message

error C2039: '_Ttype': is not a member of 'std::_Tuple_element<_Ty,std::tuple<>>'
        with
        [
            _Ty=char &
        ]

Should I expect std::get to work here, or am I missing something?

like image 248
ProbabilisticS Avatar asked Oct 23 '25 16:10

ProbabilisticS


1 Answers

I believe that this is due to the implementation of P2609R3, which changes the constraints of for_each to effectively require the following to be valid:

iter_value_t<It> v = *it;
f(proj(v));

(where proj defaults to std::identity{}, which returns the argument unchanged)

When It is zip_view's iterator, iter_value_t<It> is std::tuple<char>, which doesn't support std::get<char&> because it doesn't have a char& element.

Thus, the lambda expression doesn't support f(proj(v)) which is required by P2609R3, and MSVC is correct to emit an error.

Also note that the same code does not work with latest gcc or latest clang: https://godbolt.org/z/3Pv71rY5z.

like image 190
cpplearner Avatar answered Oct 26 '25 06:10

cpplearner



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!