std::from_r...from_range_t(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 vector from a range (public member function of std::vector<T,Allocator>)
  • constructor (C++23) β€” constructs the deque from a range (public member function of std::deque<T,Allocator>)
  • constructor (C++23) β€” constructs the forward_list from a range (public member function of std::forward_list<T,Allocator>)
  • constructor (C++23) β€” constructs the list from a range (public member function of std::list<T,Allocator>)
  • constructor (C++23) β€” constructs the set from a range (public member function of std::set<Key,Compare,Allocator>)
  • constructor (C++23) β€” constructs the map from a range (public member function of std::map<Key,T,Compare,Allocator>)
  • constructor (C++23) β€” constructs the multiset from a range (public member function of std::multiset<Key,Compare,Allocator>)
  • constructor (C++23) β€” constructs the multimap from a range (public member function of std::multimap<Key,T,Compare,Allocator>)
  • constructor (C++23) β€” constructs the unordered_set from a range (public member function of std::unordered_set<Key,Hash,KeyEqual,Allocator>)
  • constructor (C++23) β€” constructs the unordered_map from a range (public member function of std::unordered_map<Key,T,Hash,KeyEqual,Allocator>)
  • constructor (C++23) β€” constructs the unordered_multiset from a range (public member function of std::unordered_multiset<Key,Hash,KeyEqual,Allocator>)
  • constructor (C++23) β€” constructs the unordered_multimap from a range (public member function of std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>)
  • constructor (C++23) β€” constructs the priority_queue from a range (public member function of std::priority_queue<T,Container,Compare>)
  • constructor (C++23) β€” constructs the queue from a range (public member function of std::queue<T,Container>)
  • constructor (C++23) β€” constructs the stack from a range (public member function of std::stack<T,Container>)
  • constructor (C++23) β€” constructs the flat_set from a range (public member function of std::flat_set<Key,Compare,KeyContainer>)
  • constructor (C++23) β€” constructs the flat_map from a range (public member function of std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>)
  • constructor (C++23) β€” constructs the flat_multiset from a range (public member function of std::flat_multiset<Key,Compare,KeyContainer>)
  • constructor (C++23) β€” constructs the flat_multimap from a range (public member function of std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>)

πŸŽ€ Strings library πŸŽ€

  • constructor (C++23) β€” constructs the basic_string from a range (public member function of std::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