Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/ranges/from range"

From cppreference.com
< cpp‎ | ranges
m (Correct the type of from_range object)
m (+ added link to std::inplace_vector ctor)
 
(2 intermediate revisions by one user not shown)
Line 2: Line 2:
 
{{cpp/ranges/navbar}}
 
{{cpp/ranges/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | ranges}}
+
{{dcl header|ranges}}
{{dcl | since=c++23 |1=
+
{{dcl|since=c++23|1=
 
struct from_range_t { explicit from_range_t() = default; };
 
struct from_range_t { explicit from_range_t() = default; };
 
}}
 
}}
{{dcl | since=c++23 |1=
+
{{dcl|since=c++23|1=
 
inline constexpr std::from_range_t from_range {};
 
inline constexpr std::from_range_t from_range {};
 
}}
 
}}
Line 13: Line 13:
 
{{tt|std::from_range}} is a disambiguation tag that can be passed to the constructors of the suitable containers to indicate that the contained member is range constructed.
 
{{tt|std::from_range}} is a disambiguation tag that can be passed to the constructors of the suitable containers to indicate that the contained member is range constructed.
  
The corresponding type {{tt|std::from_range_t}} can be used in the constructor's parameter list to match the intended tag.
+
The corresponding type {{tt|std::from_range_t}} can be used in the constructor's parameter list to match the intended tag.
 +
 
 +
===Standard library===
 +
The following standard library types use {{tt|std::from_range_t}} type in their constructors:
 +
 
 +
{{dsc begin}}
 +
{{dsc h2|Containers library}}
 +
 
 +
{{dsc mem ctor|cpp/container/vector/vector|constructs the {{tt|vector}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/inplace_vector/inplace_vector|constructs the {{tt|inplace_vector}} from a range|notes={{mark c++26}}}}
 +
{{dsc mem ctor|cpp/container/deque/deque|constructs the {{tt|deque}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/forward_list/forward_list|constructs the {{tt|forward_list}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/list/list|constructs the {{tt|list}} from a range|notes={{mark c++23}}}}
 +
 
 +
{{dsc mem ctor|cpp/container/set/set|constructs the {{tt|set}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/map/map|constructs the {{tt|map}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/multiset/multiset|constructs the {{tt|multiset}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/multimap/multimap|constructs the {{tt|multimap}} from a range|notes={{mark c++23}}}}
 +
 
 +
{{dsc mem ctor|cpp/container/unordered_set/unordered_set|constructs the {{tt|unordered_set}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/unordered_map/unordered_map|constructs the {{tt|unordered_map}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/unordered_multiset/unordered_multiset|constructs the {{tt|unordered_multiset}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/unordered_multimap/unordered_multimap|constructs the {{tt|unordered_multimap}} from a range|notes={{mark c++23}}}}
 +
 
 +
{{dsc mem ctor|cpp/container/priority_queue/priority_queue|constructs the {{tt|priority_queue}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/queue/queue|constructs the {{tt|queue}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/stack/stack|constructs the {{tt|stack}} from a range|notes={{mark c++23}}}}
 +
 
 +
{{dsc mem ctor|cpp/container/flat_set/flat_set|constructs the {{tt|flat_set}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/flat_map/flat_map|constructs the {{tt|flat_map}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/flat_multiset/flat_multiset|constructs the {{tt|flat_multiset}} from a range|notes={{mark c++23}}}}
 +
{{dsc mem ctor|cpp/container/flat_multimap/flat_multimap|constructs the {{tt|flat_multimap}} from a range|notes={{mark c++23}}}}
 +
 
 +
{{dsc h2|Strings library}}
 +
{{dsc mem ctor|cpp/string/basic_string/basic_string|constructs the {{tt|basic_string}} from a range|notes={{mark c++23}}}}
 +
{{dsc end}}
 +
 
 +
===Notes===
 +
{{feature test macro|__cpp_lib_containers_ranges|value=202202L|std=C++23|Tagged constructors to construct from {{ls|cpp/ranges/to#container compatible range}}}}
 +
 
 +
===Example===
 +
{{example
 +
|code=
 +
#include <cassert>
 +
#include <string>
 +
 
 +
int main()
 +
{
 +
#ifdef __cpp_lib_containers_ranges
 +
    auto const range = {0x43, 43, 43};
 +
    std::string str{std::from_range, range}; // uses tagged constructor
 +
    assert(str == "C++");
 +
#endif
 +
}
 +
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/dsc to}}
+
{{dsc inc|cpp/utility/dsc in_place}}
 +
{{dsc inc|cpp/container/dsc sorted_equivalent}}
 +
{{dsc inc|cpp/container/dsc sorted_unique}}
 +
{{dsc inc|cpp/ranges/dsc to}}
 
{{dsc end}}
 
{{dsc end}}

Latest revision as of 08:00, 11 September 2024

 
 
Ranges library
Range adaptors
 
Defined in header <ranges>
struct from_range_t { explicit from_range_t() = default; };
(since C++23)
inline constexpr std::from_range_t from_range {};
(since C++23)

std::from_range is a disambiguation tag that can be passed to the constructors of the suitable containers to indicate that the contained member is range constructed.

The corresponding type std::from_range_t can be used in the constructor's parameter list to match the intended tag.

Contents

[edit] Standard library

The following standard library types use std::from_range_t type in their constructors:

Containers library
constructs the vector from a range
(public member function of std::vector<T,Allocator>)
constructs the inplace_vector from a range
(public member function of std::inplace_vector<T,N>)
constructs the deque from a range
(public member function of std::deque<T,Allocator>)
constructs the forward_list from a range
(public member function of std::forward_list<T,Allocator>)
constructs the list from a range
(public member function of std::list<T,Allocator>)
constructs the set from a range
(public member function of std::set<Key,Compare,Allocator>)
constructs the map from a range
(public member function of std::map<Key,T,Compare,Allocator>)
constructs the multiset from a range
(public member function of std::multiset<Key,Compare,Allocator>)
constructs the multimap from a range
(public member function of std::multimap<Key,T,Compare,Allocator>)
constructs the unordered_set from a range
(public member function of std::unordered_set<Key,Hash,KeyEqual,Allocator>)
constructs the unordered_map from a range
(public member function of std::unordered_map<Key,T,Hash,KeyEqual,Allocator>)
constructs the unordered_multiset from a range
(public member function of std::unordered_multiset<Key,Hash,KeyEqual,Allocator>)
constructs the unordered_multimap from a range
(public member function of std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>)
constructs the priority_queue from a range
(public member function of std::priority_queue<T,Container,Compare>)
constructs the queue from a range
(public member function of std::queue<T,Container>)
constructs the stack from a range
(public member function of std::stack<T,Container>)
constructs the flat_set from a range
(public member function of std::flat_set<Key,Compare,KeyContainer>)
constructs the flat_map from a range
(public member function of std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>)
constructs the flat_multiset from a range
(public member function of std::flat_multiset<Key,Compare,KeyContainer>)
constructs the flat_multimap from a range
(public member function of std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>)
Strings library
constructs the basic_string from a range
(public member function of std::basic_string<CharT,Traits,Allocator>)

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_containers_ranges 202202L (C++23) Tagged constructors to construct from container compatible range

[edit] Example

#include <cassert>
#include <string>
 
int main()
{
#ifdef __cpp_lib_containers_ranges
    auto const range = {0x43, 43, 43};
    std::string str{std::from_range, range}; // uses tagged constructor
    assert(str == "C++");
#endif
}

[edit] See also

in-place construction tag
(tag)[edit]
indicates that elements of a range are sorted (uniqueness is not required)
(tag)[edit]
indicates that elements of a range are sorted and unique
(tag)[edit]
constructs a new non-view object from an input range
(function template) [edit]