Namespaces
Variants

deduction guides for std::basic_string_view

From cppreference.net
Definiert im Header <string_view>
template < class It, class End >
basic_string_view ( It, End ) - > basic_string_view < std:: iter_value_t < It >> ;
(1) (seit C++20)
template < class R >
basic_string_view ( R && ) - > basic_string_view < ranges:: range_value_t < R >> ;
(2) (seit C++23)

Diese Deduction Guides werden für std::basic_string_view bereitgestellt.

1) Diese Deduktion-Anleitung erlaubt es, den Zeichentyp aus dem Iterator-Sentinel-Paar abzuleiten. Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn It die Bedingung contiguous_iterator erfüllt und End die Bedingung sized_sentinel_for für It erfüllt.
2) Diese Deduction Guide ermöglicht es, den Zeichentyp aus dem Bereich abzuleiten. Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn R die Bedingung contiguous_range erfüllt.

Beispiel

#include <array>
#include <iostream>
#include <string_view>
int main()
{
    std::array a1{'n', 'u', 'c', 'l', 'e', 'o', 'n', 's', ':', '\n'};
    std::basic_string_view s1(a1.cbegin(), a1.cend()); // Ableitung: CharT -> char
    static_assert(std::is_same_v<decltype(s1)::value_type, char>);
    std::cout << s1;
    std::array a2{L'p', L'r', L'o', L't', L'o', L'n', L's', L'\n'};
    std::basic_string_view s2(a2.cbegin(), a2.cend()); // Ableitung: CharT -> wchar_t
    static_assert(std::is_same_v<decltype(s2)::value_type, wchar_t>);
    std::wcout << s2;
    std::array<long, 9> a3{'n', 'e', 'u', 't', 'r', 'o', 'n', 's', '\n'};
    std::basic_string_view s3(a3.cbegin(), a3.cend()); // Ableitung: CharT -> long
    static_assert(std::is_same_v<decltype(s3)::value_type, long>);
    for (const auto e : s3)
        std::cout << static_cast<char>(e);
}

Ausgabe:

nucleons:
protons
neutrons