#1813
Error when using function return value directly
version: 0.12.0 compiler: Visual Studio 2022 x64
code:
#include <vector>
#include <range/v3/view.hpp>
#include <range/v3/range.hpp>
namespace rv = ranges::views;
std::vector<int> getAllItems()
{
return {};
}
int main()
{
std::vector<int> allItems;
auto tt = getAllItems()
| rv::filter(
[](auto item)
{
return item % 2 == 1;
})
| ranges::to<std::vector<int>>();
return 0;
}error logs:
Build started at 16:43...
1>------ Build started: Project: ConsoleApplication_141_03, Configuration: Debug x64 ------
1>ConsoleApplication_141_03.cpp
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2280: 'std::vector<int,std::allocator<int>> ranges::views::view_closure_base_ns::operator |<std::vector<int,std::allocator<int>>,ranges::detail::bind_back_fn_<ranges::views::filter_base_fn,main::<lambda_1>>>(Rng &&,const ranges::views::view_closure<ranges::detail::bind_back_fn_<ranges::views::filter_base_fn,main::<lambda_1>>> &)': attempting to reference a deleted function
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2280: with
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2280: [
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2280: Rng=std::vector<int,std::allocator<int>>
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2280: ]
1>C:\Users\xxx\.conan\data\range-v3\0.12.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\range\v3\view\view.hpp(127,13):
1>see declaration of 'ranges::views::view_closure_base_ns::operator |'
1>F:\TestProject\ConsoleApplication_141_03\ConsoleApplication_141_03\ConsoleApplication_141_03.cpp(15,9): error C2088: '|': illegal for class
1>Done building project "ConsoleApplication_141_03.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 16:43 and took 01.702 seconds ==========Activity
2In c++20 this works because of p2415 as mentioned in #1725
This is intended behaviour
Unfortunately, it looks like msvc doesn't print the helpful stuff that gcc or clang would in the error message:
And the referenced code section:
See https://gcc.godbolt.org/z/s58o7bjqT
getAllItems()returns an rvaluestd::vector, andrv::filteris a view, ie. non owningThe issue with this code working would be where the rvalue vector would live, what would own it - views are lightweight and don't own containers like this (except cache1 AFAIK) so the filter view can't. Nothing can.
| ranges::to<std::vector<int>>();can be removed and you'll get the same errorAs you've alluded to in your code snippet, and the above compile message says, a fix here is to use an lvalue instead