Namespaces
Variants

std::ranges:: elements_of

From cppreference.net
Ranges library
Range adaptors
헤더에 정의됨 <ranges>
template < ranges:: range R, class Allocator = std:: allocator < std:: byte > >
struct elements_of ;
(C++23부터)

range 를 캡슐화합니다. elements_of 의 특수화는 범위를 단일 값이 아닌 시퀀스로 처리해야 할 때 오버로드 집합에서 태그 역할을 수행합니다.

목차

템플릿 매개변수

R - range 요구 사항을 충족하는 타입
Allocator - Allocator 요구 사항을 충족하는 할당자 타입

데이터 멤버

멤버 이름 정의
range
R 타입의 범위
(공개 멤버 객체)
allocator
Allocator 타입의 할당자. 자신을 값 초기화하는 기본 멤버 초기화자를 가짐
(공개 멤버 객체)

이 모든 멤버들은 [[ no_unique_address ]] 속성으로 선언되었습니다.

추론 가이드

template < class R, class Allocator = std:: allocator < std:: byte > >
elements_of ( R && , Allocator = Allocator ( ) ) - > elements_of < R && , Allocator > ;
(C++23부터)

예제

#include <any>
#include <generator>
#include <iostream>
#include <ranges>
#include <string_view>
template<bool Elementwise>
std::generator<std::any> gen(std::ranges::input_range auto&& r)
{
    if constexpr (Elementwise)
        co_yield std::ranges::elements_of(r); // r의 각 요소를 yield
    else
        co_yield r;                           // r을 단일 값으로 yield
}
int main()
{
    auto test = std::string_view{"test"};
    for (std::any a : gen<true>(test))
        std::cout << '[' << std::any_cast<char>(a) << "] ";
    std::cout << '\n';
    for (std::any a : gen<false>(test))
        std::cout << '[' << std::any_cast<std::string_view>(a) << "] ";
    std::cout << '\n';
}

출력:

[t] [e] [s] [t] 
[test]

참조문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 26.5.6 클래스 템플릿 elements_of [range.elementsof]