error: 'to' is not a member of 'std::ranges'
Asked Answered
A

1

5

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

Agulhas answered 2/12, 2022 at 22:33 Comment(0)
B
11

This is because std::ranges::to is only supported right now by MSVC 19.34 and Clang (libc++) 17

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
Boling answered 2/12, 2022 at 22:38 Comment(1)
Notice, meanwhile, you can use range-v3 library's ranges::to: godbolt.org/z/6fYx87v4KBedplate

© 2022 - 2024 — McMap. All rights reserved.