std::initializer_list
出自 cppreference.com
(請勿與 成員初始化列表 (member initializer list) 混淆)
| 定義於標頭檔 <initializer_list> |
||
| template< class T > class initializer_list; |
(C++11 起) | |
std::initializer_list<T> 型別的物件是一個輕量級代理物件,提供對 const T 型別物件陣列的存取(該陣列可能配置在唯讀記憶體中)。
當以下情況發生時,會自動建構 std::initializer_list 物件:
- 使用大括號初始化列表對物件進行列表初始化 (list-initialization),且對應的建構函式接受一個
std::initializer_list參數時, - 大括號初始化列表被用作賦值運算的右運算元,或作為函式呼叫引數,且對應的賦值運算子/函式接受一個
std::initializer_list參數時, - 大括號初始化列表被繫結至 auto 時,包含在範圍基礎的 for 迴圈 (ranged for loop) 中。
std::initializer_list 可能被實作為一對指標或指標加長度。複製 std::initializer_list 並不會複製對應初始化列表的後備陣列 (backing array)。
若宣告 std::initializer_list 的顯式或部分特化,則程式格式不正確。
目錄 |
[編輯] 成員型別
| 名稱 | 定義 |
value_type
|
T
|
reference
|
const T& |
const_reference
|
const T& |
size_type
|
std::size_t |
iterator
|
const T* |
const_iterator
|
const T* |
[編輯] 成員函式
| 建立一個空的初始化列表 (公開成員函式) | |
容量 | |
| 傳回初始化列表中的元素數量 (公開成員函式) | |
迭代器 | |
| 傳回指向第一個元素的指標 (公開成員函式) | |
| 傳回指向最後一個元素之後位置的指標 (公開成員函式) | |
[編輯] 非成員函式
| 多載 std::begin (函式模板) | |
| 特化 std::end (函式模板) | |
針對
| |
| (C++14) |
傳回指向容器或陣列起點的反向迭代器 (函式模板) |
| (C++14) |
傳回容器或陣列的反向末端迭代器 (函式模板) |
| (C++17) |
檢查容器是否為空 (函式模板) |
| (C++17) |
取得指向底層陣列的指標 (函式模板) |
[編輯] 附註
| 特性測試巨集 | 數值 | 標準 | 功能 |
|---|---|---|---|
__cpp_initializer_lists |
200806L |
(C++11) | 列表初始化 (List-initialization) 與 std::initializer_list |
[編輯] 範例
執行此程式碼
#include <cassert> #include <initializer_list> #include <iostream> #include <vector> template<class T> struct S { std::vector<T> v; S(std::initializer_list<T> l) : v(l) { std::cout << "constructed with a " << l.size() << "-element list\n"; } void append(std::initializer_list<T> l) { v.insert(v.end(), l.begin(), l.end()); } std::pair<const T*, std::size_t> c_arr() const { return {&v[0], v.size()}; // copy list-initialization in return statement // this is NOT a use of std::initializer_list } }; template<typename T> void templated_fn(T) {} int main() { S<int> s = {1, 2, 3, 4, 5}; // copy list-initialization s.append({6, 7, 8}); // list-initialization in function call std::cout << "The vector now has " << s.c_arr().second << " ints:\n"; for (auto n : s.v) std::cout << n << ' '; std::cout << '\n'; std::cout << "Range-for over brace-init-list: \n"; for (int x : {-1, -2, -3}) // the rule for auto makes this ranged-for work std::cout << x << ' '; std::cout << '\n'; auto al = {10, 11, 12}; // special rule for auto std::cout << "The list bound to auto has size() = " << al.size() << '\n'; auto la = al; // a shallow-copy of top-level proxy object assert(la.begin() == al.begin()); // guaranteed: backing array is the same std::initializer_list<int> il{-3, -2, -1}; assert(il.begin()[2] == -1); // note the replacement for absent operator[] il = al; // shallow-copy assert(il.begin() == al.begin()); // guaranteed // templated_fn({1, 2, 3}); // compiler error! "{1, 2, 3}" is not an expression, // it has no type, and so T cannot be deduced templated_fn<std::initializer_list<int>>({1, 2, 3}); // OK templated_fn<std::vector<int>>({1, 2, 3}); // also OK }
輸出
constructed with a 5-element list The vector now has 8 ints: 1 2 3 4 5 6 7 8 Range-for over brace-init-list: -1 -2 -3 The list bound to auto has size() = 3
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯應用於之前的 C++ 標準。
| DR | 應用於 | 出版時的行為 | 正確的行為 |
|---|---|---|---|
| LWG 2129 | C++11 | std::initializer_list 是否可以有顯式特化或部分特化 |
程式是 格式錯誤 (ill-formed) |
[編輯] 參見
| (C++20) |
連續物件序列上的非擁有型視圖 (類別樣板) |
| (C++17) |
唯讀字串檢視 (string view) (類別樣板) |