名稱空間
變體
操作

std::list 的推導指南

來自 cppreference.com
< cpp‎ | 容器‎ | list
 
 
 
 
定義於標頭檔案 <list>
template< class InputIt,

          class Alloc = std::allocator<
              typename std::iterator_traits<InputIt>::value_type> >
list( InputIt, InputIt, Alloc = Alloc() )

    -> list<typename std::iterator_traits<InputIt>::value_type, Alloc>;
(1) (C++17 起)
template< ranges::input_range R,

          class Alloc = std::allocator<ranges::range_value_t<R>> >
list( std::from_range_t, R&&, Alloc = Alloc() )

    -> list<ranges::range_value_t<R>, Alloc>;
(2) (C++23 起)
1)推導指南為 list 提供,允許從迭代器範圍進行推導。此過載僅在 InputIt 滿足 LegacyInputIteratorAlloc 滿足 Allocator 時參與過載決議。
2) 此推導指南為 list 提供,允許從 std::from_range_t 標籤和 input_range 進行推導。

注意:庫確定型別不滿足 LegacyInputIterator 的程度未指定,但至少整數型別不符合輸入迭代器的條件。同樣,庫確定型別不滿足 Allocator 的程度也未指定,但至少成員型別 Alloc::value_type 必須存在,並且表示式 std::declval<Alloc&>().allocate(std::size_t{}) 在被視為未求值運算元時必須格式良好。

[編輯] 注意

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 範圍感知構造與插入;過載 (2)

[編輯] 示例

#include <list>
#include <vector>
 
int main()
{
    std::vector<int> v = {1, 2, 3, 4};
 
    // uses explicit deduction guide to deduce std::list<int>
    std::list x(v.begin(), v.end());
 
    // deduces std::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::list y{v.begin(), v.end()};
}