Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

error: 'to' is not a member of 'std::ranges'

Facing issue
std::ranges::to
I am executing the below example from https://en.cppreference.com/w/cpp/ranges/to

#include <algorithm>
#include <concepts>
#include <iostream>
#include <ranges>
#include <vector>
 
int main()
{
    auto vec = std::views::iota(1, 5)
             | std::views::transform([](auto const v){ return v * 2; })
             | std::ranges::to<std::vector>();
 
    static_assert(std::same_as<decltype(vec), std::vector<int>>);
 
    std::ranges::for_each(vec, [](auto const v){ std::cout << v << ' '; });
}

But getting a error

main.cpp: In function 'int main()':
main.cpp:11:29: error: 'to' is not a member of 'std::ranges'
   11 |              | std::ranges::to<std::vector>();
      |                             ^~
main.cpp:11:43: error: missing template arguments before '>' token
   11 |              | std::ranges::to<std::vector>();
      |                                           ^
main.cpp:11:45: error: expected primary-expression before ')' token
   11 |              | std::ranges::to<std::vector>();
      |                                             ^
main.cpp:13:24: error: template argument 1 is invalid
   13 |     static_assert(std::same_as<decltype(vec), std::vector<int>>);

https://coliru.stacked-crooked.com/view?id=8fdd3554af82ef24

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using the compiler C++23

>Solution :

This is because std::ranges::to is only supported right now by MSVC 19.34

You can check on the status of compiler support for language and library features here: https://en.cppreference.com/w/cpp/compiler_support

For example this feature is listed un the C++23 library section as

C++23 feature Paper(s)
ranges::to() P1206R7
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading