Namespaces
Variants

deduction guides for std::forward_list

From cppreference.net

ヘッダーで定義 <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) この deduction guide は、forward_listがイテレータ範囲からの推論を可能にするために提供されています。このオーバーロードは、 InputIt LegacyInputIterator を満たし、かつ Alloc Allocator を満たす場合にのみ、オーバーロード解決に参加します。
2) この推論ガイドは、forward_listが std::from_range_t タグと input_range からの推論を可能にするために提供されています。

注意: ライブラリが型が LegacyInputIterator を満たさないと判断する範囲は未規定であるが、少なくとも整数型は入力イテレータとして適格ではない。同様に、型が Allocator を満たさないと判断する範囲も未規定であるが、少なくともメンバ型 Alloc::value_type が存在しなければならず、式 std:: declval < Alloc & > ( ) . allocate ( std:: size_t { } ) が未評価オペランドとして扱われた場合に well-formed でなければならない。

注記

機能テスト マクロ 標準 機能
__cpp_lib_containers_ranges 202202L (C++23) レンジ対応 構築と挿入; オーバーロード (2)

#include <forward_list>
#include <vector>
int main()
{
    std::vector<int> v = {1, 2, 3, 4};
    // 明示的なdeduction guideを使用してstd::forward_list<int>を推論
    std::forward_list x(v.begin(), v.end());
    // std::forward_list<std::vector<int>::iterator>を推論
    // リスト初期化のオーバーロード解決の第一段階では、初期化子リストコンストラクタから
    // 合成された候補が選択され、第二段階は実行されずdeduction guideは効果を持たない
    std::forward_list y{v.begin(), v.end()};
}