std::forward_list
的推導指南
來自 cppreference.com
< cpp | 容器 | forward_list
在標頭檔案 <forward_list> 中定義 |
||
template< class InputIt, class Alloc = std::allocator< |
(1) | (C++17 起) |
template< ranges::input_range R, class Alloc = std::allocator<ranges::range_value_t<R>> > |
(2) | (C++23 起) |
1) 為 forward_list 提供此推導指南,以允許從迭代器範圍進行推導。此過載僅在
InputIt
滿足 LegacyInputIterator 且 Alloc
滿足 Allocator 時參與過載決議。注意:庫確定型別不滿足 LegacyInputIterator 的程度未指定,但至少積分型別不符合輸入迭代器的條件。同樣,庫確定型別不滿足 Allocator 的程度未指定,但至少成員型別 Alloc::value_type
必須存在,並且表示式 std::declval<Alloc&>().allocate(std::size_t{}) 在被視為未求值運算元時必須是格式良好的。
[編輯] 注意
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 範圍感知構造與插入;過載 (2) |
[編輯] 示例
執行此程式碼
#include <forward_list> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4}; // uses explicit deduction guide to deduce std::forward_list<int> std::forward_list x(v.begin(), v.end()); // deduces std::forward_list<std::vector<int>::iterator> // first phase of overload resolution for list-initialization selects the candidate // synthesized from the initializer-list constructor; second phase is not performed // and deduction guide has no effect std::forward_list y{v.begin(), v.end()}; }