The code below causes the following IntelliSense warning in Visual Studio 2022 (latest version with /std:c++latest):
Warning C26800 Use of a moved from object: ''$S2'' (lifetime.1)
for line with std::ranges::equal_range
.
Is this false-positive or there is some real issue in the code?
The issue comes only when assigned to the auto [it_lower, it_upper]
; in case it is assigned to range
, no warnings come. It doesn’t matter if applied to the range created based on view (as in the example) or to any other container, I used this syntax only for sake of brevity.
The code
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main()
{
auto range = std::ranges::iota_view{ 1, 100 };
auto [it_lower, it_upper] = std::ranges::equal_range(range, 10, {}, {});
while (it_lower != it_upper) {
++it_lower;
}
}
No any issues with compilation, running and checking. No problems on godbolt both with clang and msvc.
Demos
Let me try, with my rather limited knowledge, to answer this.
First, according to cppreference:
The returned view is constructed from two iterators, one pointing to the first element that is not less than value and another pointing to the first element greater than value.
So far so good, just make sure the range is properly sorted.
As for any complications introduced by using a structured binding, this article helped me get my head around that:
https://jguegant.github.io/blogs/tech/structured-bindings.html
So, tl;dr; I think it's OK and Intellisense is just crying wolf.