名前空間
変種
操作

std::forward_list の推論ガイド

From cppreference.com
 
 
 
 
ヘッダ <forward_list> で定義
template< class InputIt,

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

    -> forward_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>> >
forward_list( std::from_range_t, R&&, Alloc = Alloc() )

    -> forward_list<ranges::range_value_t<R>, Alloc>;
(2) (C++23から)
1) この 推論ガイド は、イテレータ範囲からの推論を可能にするために forward_list に対して提供されます。このオーバーロードは、InputItLegacyInputIterator を満たし、AllocAllocator を満たす場合にのみ、オーバーロード解決に参加します。
2) この推論ガイドは、std::from_range_t タグと input_range からの推論を可能にするために forward_list に対して提供されます。

注意: ライブラリが型が 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()};
}
English 日本語 中文(简体) 中文(繁體)