forward_list(); // (1) C++14
explicit forward_list(const Allocator& a); // (2) C++14
explicit forward_list(const Allocator& a = Allocator()); // (1) + (2) C++11
forward_list(size_type n,
const T& value,
const Allocator& a = Allocator()); // (3) C++11
explicit forward_list(size_type n); // (4) C++11
explicit forward_list(size_type n,
const Allocator& a = Allocator()); // (4) C++14
template <class InputIterator>
forward_list(InputIterator first,
InputIterator last,
const Allocator& a = Allocator()); // (5) C++11
forward_list(const forward_list& x); // (6) C++11
forward_list(forward_list&& x); // (7) C++11
forward_list(const forward_list& x, const Allocator& a); // (8) C++11
forward_list(forward_list&& x, const Allocator& a); // (9) C++11
forward_list(initializer_list<T> il,
const Allocator& a = Allocator()); // (10) C++11
template <container-compatible-range<T> R>
forward_list(from_range_t, R&& rg,
const Allocator& a = Allocator()); // (11) C++23
概要
forward_listオブジェクトを、以下に示す通りの要素で初期化する。
効果
- (1) : デフォルトコンストラクタ。空のコンテナを作る。
- (2) : アロケータを指定して空のコンテナを作る。
- (1) + (2) : デフォルトコンストラクタ。アロケータを指定して空のコンテナを作る。
- (3) :
valueのコピーをn個要素として保持したforward_listオブジェクトを構築する。 - (4)
- C++11 :
n個のT()で初期化された要素を保持したforward_listオブジェクトを構築する。 - C++14 : アロケータを指定して
n個のT()で初期化された要素を保持したforward_listオブジェクトを構築する。
- C++11 :
- (5) : イテレータ範囲
[first, last)を要素としてコピーしたforward_listオブジェクトを構築する。 - (6) : コピーコンストラクタ。
xと同じ要素を保持したforward_listオブジェクトを構築する。 - (7) : ムーブコンストラクタ。
xの指す先を自分の領域としてforward_listオブジェクトを構築する。 - (8) : アロケータを指定したコピーコンストラクタ
- (9) : アロケータを指定したムーブコンストラクタ
- (10) : 初期化子リストを受け取るコンストラクタ。
forward_list(il.begin(), il.end(), a)と等価。 - (11) : Range
rgの要素をコピーしたforward_listオブジェクトを構築する。
計算量
- (1), (2) : 定数時間
- (3) :
nに対して線形時間 - (4) :
nに対して線形時間 - (5) :
distance(first, last)に対して線形時間 - (6) :
xの要素数に対して線形時間 - (7) : 定数時間
- (8) :
xの要素数に対して線形時間 - (9) :
x.get_allocator() == aであれば、定数時間。そうでなければxの要素数に対して線形時間 - (10) :
ilの要素数に対して線形時間 - (11) :
rgの要素数に対して線形時間
備考
- イテレータ範囲コンストラクタ
template <class InputIterator> forward_list(InputIterator first, InputIterator last, const Allocator& a = Allocator())は、InputIteratorが入力イテレータの要件を満たさなければオーバーロード解決に参加しない。 -
C++14 では、
explicit forward_list(const Allocator& a = Allocator())がデフォルト引数を使用しない 2 つのオーバーロードに分割された。
これは、デフォルトコンストラクタにexplicitが付いていると、std::forward_list<int> fl = {};のようなコード(C++11 から導入された、コピーリスト初期化によるデフォルトコンストラクタ呼び出し)がエラーになってしまうためである。
-
C++14 では、
explicit forward_list(size_type n)に引数が追加され、explicit forward_list(size_type n, const Allocator& a = Allocator())に変更された。
これは、変更されないとnのみを引数にとるアロケータ使用構築(uses-allocator construction)に失敗してしまうためである。 具体的には、C++11 では以下のようなコードがエラーになってしまう。#include <list> #include <forward_list> #include <scoped_allocator> int main() { using fli = std::forward_list<int>; std::list<fli, std::scoped_allocator_adaptor<std::allocator<fli>>> l; l.emplace_back(10u); }
例
#include <iostream>
#include <forward_list>
#include <utility>
template <class T>
void print(const char* name, const std::forward_list<T>& ls)
{
std::cout << name << " : ";
for (const T& x : ls) {
std::cout << x << ' ';
}
std::cout << std::endl;
}
int main()
{
// (1) デフォルト構築
std::forward_list<int> ls1;
print("ls1", ls1);
// (4) n個の要素を持つリストを作成
std::forward_list<int> ls2(3);
print("ls2", ls2);
// (3) n個の指定された値を要素を持つリストを作成
std::forward_list<int> ls3(3, 1);
print("ls3", ls3);
// (5) 範囲から構築
std::forward_list<int> ls4(ls3.begin(), ls3.end());
print("ls4", ls4);
// (6) コピー構築
std::forward_list<int> ls5 = ls4;
print("ls5", ls5);
// (7) ムーブ構築
std::forward_list<int> ls6 = std::move(ls5);
print("ls6", ls6);
// (10) 初期化子リストで構築
std::forward_list<int> ls7 = {1, 2, 3};
print("ls7", ls7);
}
出力
ls1 :
ls2 : 0 0 0
ls3 : 1 1 1
ls4 : 1 1 1
ls5 : 1 1 1
ls6 : 1 1 1
ls7 : 1 2 3
バージョン
言語
- C++11
処理系
- Clang: ??
- GCC: 4.7.0 ✅
- ICC: ??
- Visual C++: 2010 ✅, 2012 ✅, 2013 ✅, 2015 ✅, 2017 ✅
参照
- N2679 Initializer Lists for Standard Containers(Revision 1)
- (10)の経緯となる提案文書
- LWG 2193. Default constructors for standard library containers are explicit
explicit forward_list(const Allocator& a = Allocator())を 2 つのオーバーロードに分割するきっかけとなったレポート - LWG 2210. Missing allocator-extended constructor for allocator-aware containers
explicit forward_list(size_type n)にアロケータ引数を追加するきっかけとなったレポート
なお、Discussion の例はアロケータの型が誤っているので注意