the call expression, and is an rvalue otherwise. Thus  std  ::  move  (  g  )  (  r  )  can move the bound arguments into the call, where  g  (  r  )  would copy. 
  * The specified template argument is  (3)  ` C ` or  (4)  the deduced type from a class template ` C ` that must not satisfy [ ` view ` ](/w/cpp/ranges/view "cpp/ranges/view") . 

The program is ill-formed if ` g ` has volatile-qualified type.

###  [ [ edit ](/mwiki/index.php?title=cpp/ranges/to&action=edit&section=6
"Edit section: Exceptions") ]  Exceptions

Only throws if construction of a non-view object throws.

###  [ [ edit ](/mwiki/index.php?title=cpp/ranges/to&action=edit&section=7
"Edit section: Notes") ]  Notes

The insertion of elements into the container may involve copy which can be
less efficient than move because lvalue references are produced during the
indirection call. Users can opt-in to use [ ` views::as_rvalue `
](/w/cpp/ranges/as_rvalue_view "cpp/ranges/as rvalue view") to adapt the range
in order for their elements to always produce an rvalue reference during the
indirection call which implies move.

The parentheses are mandatory when using the pipe syntax.

    
    
    auto vec = r | std::ranges::to<[std::vector](http://en.cppreference.com/w/cpp/container/vector)>;   // error
    auto vec = r | std::ranges::to<[std::vector](http://en.cppreference.com/w/cpp/container/vector)>(); // OK

[ Feature-test ](/w/cpp/utility/feature_test "cpp/utility/feature test") macro  |  Value  |  Std  |  Feature   
---|---|---|---  
[ ` __cpp_lib_ranges_to_container ` ](/w/cpp/feature_test#cpp_lib_ranges_to_container "cpp/feature test") |  202202L  |  (C++23)  |  ` std::ranges::to `  
  
###  [ [ edit ](/mwiki/index.php?title=cpp/ranges/to&action=edit&section=8
"Edit section: Example") ]  Example

A preview link: [ Compiler Explorer ](https://godbolt.org/z/TKKqqPx7x)

Run this code

    
    
    #include <concepts>
    #include <list>
    #include <print>
    #include <ranges>
    #include <vector>
     
    int main()
    {
        auto vec = std::[views::iota](http://en.cppreference.com/w/cpp/ranges/iota_view)(1, 5)
                 | std::[views::transform](http://en.cppreference.com/w/cpp/ranges/transform_view)([](auto const v){ return v * 2; })
                 | std::ranges::to<[std::vector](http://en.cppreference.com/w/cpp/container/vector)>();
     
        static_assert([std::same_as](http://en.cppreference.com/w/cpp/concepts/same_as)<decltype(vec), [std::vector](http://en.cppreference.com/w/cpp/container/vector)<int>>);
        [std::println](http://en.cppreference.com/w/cpp/io/println)("{}", vec);
     
        auto lst = vec | std::[views::take](http://en.cppreference.com/w/cpp/ranges/take_view)(3) | std::ranges::to<[std::list](http://en.cppreference.com/w/cpp/container/list)<double>>();
        [std::println](http://en.cppreference.com/w/cpp/io/println)("{}", lst);
    }

Output:

    
    
    [2, 4, 6, 8]
    [2, 4, 6]

###  [ [ edit ](/mwiki/index.php?title=cpp/ranges/to&action=edit&section=9
"Edit section: References") ]  References

  * C++23 standard (ISO/IEC 14882:2023): 

    

  * 26.5.7 Range conversions [range.utility.conv] 

###  [ [ edit ](/mwiki/index.php?title=cpp/ranges/to&action=edit&section=10
"Edit section: Defect reports") ]  Defect reports

The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.

DR  |  Applied to  |  Behavior as published  |  Correct behavior   
---|---|---|---  
[ LWG 3984 ](https://cplusplus.github.io/LWG/issue3984) |  C++23  |  the nested construction branch of ` ranges::to `   
resulted to program ill-formed if  R  & does not model [ ` viewable_range ` ](/w/cpp/ranges/viewable_range "cpp/ranges/viewable range") |  made well-formed   
[ LWG 4016 ](https://cplusplus.github.io/LWG/issue4016) |  C++23  |  the container insertion branch of ` ranges::to ` involved use of   
insert iterators  |  replaced with direct appending   
of elements to containe