std::basic_string_view 的推导指引
出自cppreference.com
| 在標頭 <string_view> 定義
|
||
| |
(1) | (C++20 起) |
| |
(2) | (C++23 起) |
為 std::basic_string_view 提供這些推導指引。
1) 此推導指引允許從迭代器-哨位對推導字符類型。此重載只有在
It 滿�� contiguous_iterator 且 End 滿足 It 的 sized_sentinel_for 時才會參與重載決議。示例
運行此代碼
#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()); // 推导: 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()); // 推导: 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()); // 推导: CharT -> long
static_assert(std::is_same_v<decltype(s3)::value_type, long>);
for (const auto e : s3)
std::cout << static_cast<char>(e);
}
輸出:
nucleons:
protons
neutrons