std::from_r...from_range_t(3)
π std::from_range, std::from_range_t
ok so bestie, this one lives in the C++ Standard Library β it's the std::from_range / std::from_range_t page from cppreference (we love a cppreference moment π«Ά), and it's honestly giving "small but powerful" main character energy β¨
β¨ NAME β¨
std::from_range, std::from_range_t β literally the tag squad that tells your container "bestie, build yourself from this range okay" π
πΈ SYNOPSIS πΈ
It's all defined in the header <ranges>, slay:
struct from_range_t { explicit from_range_t() = default; }; (since C++23)
inline constexpr std::from_range_t from_range {}; (since C++23)
So std::from_range is a disambiguation tag that you can pass to the constructors of the suitable containers, and it says "hey, the contained member is range constructed" π
And the matching type std::from_range_t is what you use in the constructor's parameter list to match the intended tag. They're a matching set, no cap π
π Standard library π
The following standard library types use the std::from_range_t type in their constructors β get ready, it's a long list and it ate:
π§Ί Containers library π§Ί
- constructor (C++23) β constructs the
vectorfrom a range (public member function ofstd::vector<T,Allocator>) - constructor (C++23) β constructs the
dequefrom a range (public member function ofstd::deque<T,Allocator>) - constructor (C++23) β constructs the
forward_listfrom a range (public member function ofstd::forward_list<T,Allocator>) - constructor (C++23) β constructs the
listfrom a range (public member function ofstd::list<T,Allocator>) - constructor (C++23) β constructs the
setfrom a range (public member function ofstd::set<Key,Compare,Allocator>) - constructor (C++23) β constructs the
mapfrom a range (public member function ofstd::map<Key,T,Compare,Allocator>) - constructor (C++23) β constructs the
multisetfrom a range (public member function ofstd::multiset<Key,Compare,Allocator>) - constructor (C++23) β constructs the
multimapfrom a range (public member function ofstd::multimap<Key,T,Compare,Allocator>) - constructor (C++23) β constructs the
unordered_setfrom a range (public member function ofstd::unordered_set<Key,Hash,KeyEqual,Allocator>) - constructor (C++23) β constructs the
unordered_mapfrom a range (public member function ofstd::unordered_map<Key,T,Hash,KeyEqual,Allocator>) - constructor (C++23) β constructs the
unordered_multisetfrom a range (public member function ofstd::unordered_multiset<Key,Hash,KeyEqual,Allocator>) - constructor (C++23) β constructs the
unordered_multimapfrom a range (public member function ofstd::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>) - constructor (C++23) β constructs the
priority_queuefrom a range (public member function ofstd::priority_queue<T,Container,Compare>) - constructor (C++23) β constructs the
queuefrom a range (public member function ofstd::queue<T,Container>) - constructor (C++23) β constructs the
stackfrom a range (public member function ofstd::stack<T,Container>) - constructor (C++23) β constructs the
flat_setfrom a range (public member function ofstd::flat_set<Key,Compare,KeyContainer>) - constructor (C++23) β constructs the
flat_mapfrom a range (public member function ofstd::flat_map<Key,T,Compare,KeyContainer,MappedContainer>) - constructor (C++23) β constructs the
flat_multisetfrom a range (public member function ofstd::flat_multiset<Key,Compare,KeyContainer>) - constructor (C++23) β constructs the
flat_multimapfrom a range (public member function ofstd::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>)
π Strings library π
- constructor (C++23) β constructs the
basic_stringfrom a range (public member function ofstd::basic_string<CharT,Traits,Allocator>)
π¦ Notes π¦
the feature-test macro situation, iykyk:
- Feature-test macro:
__cpp_lib_containers_ranges - Value:
202202L - Std: (C++23)
- Feature: Tagged constructors to construct from container compatible range
β Example β
// Run this 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
}
like?? {0x43, 43, 43} becoming "C++"?? the way that just works is iconic, honestly ππ
π See also π
in_place,in_place_type,in_place_index,in_place_t,in_place_type_t,in_place_index_t(C++17) β in-place construction tag (tag)sorted_equivalent,sorted_equivalent_t(C++23) β indicates that elements of a range are sorted (uniqueness is not required) (tag)sorted_unique,sorted_unique_t(C++23) β indicates that elements of a range are sorted and unique (tag)ranges::to(C++23) β constructs a new non-view object from an input range (function template)
ok that's the whole page bestie, you're so welcome π this was pulled straight from cppreference.com, dated 2024.06.10 β go forth and range-construct responsibly, mwah π§β¨πΈ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms