Home > OS >  error: 'to' is not a member of 'std::ranges'
error: 'to' is not a member of 'std::ranges'

Time:12-03

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

I am using the compiler C 23

CodePudding user response:

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
  • Related