名前空間
変種
操作

std::initializer_list

From cppreference.com
< cpp‎ | utility
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
initializer_list
(C++11)
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 

メンバ初期化子リストと混同しないこと)

ヘッダ <initializer_list> で定義
template< class T >
class initializer_list;
(C++11以降)

std::initializer_list<T> 型のオブジェクトは、const T 型のオブジェクトの配列(読み取り専用メモリに配置される可能性がある)へのアクセスを提供する軽量なプロキシオブジェクトである。

std::initializer_list オブジェクトは、以下の場合に自動的に構築される。

std::initializer_list は、ポインタのペア、またはポインタと長さとして実装されうる。std::initializer_list をコピーしても、対応する初期化子リストの背後にある配列はコピーされない。

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*

[編集] メンバ関数

空の初期化子リストを作成する
(public member function) [編集]
容量
初期化子リスト内の要素数を返す
(public member function) [編集]
イテレータ
最初の要素へのポインタを返す
(public member function) [編集]
最後の要素の次を指すポインタを返す
(public member function) [編集]

[編集] 非メンバ関数

std::begin をオーバーロードする
(function template) [編集]
std::end を特殊化する
(function template) [編集]
std::initializer_list 用にオーバーロードされた自由関数テンプレート
コンテナまたは配列の先頭を指す逆順イテレータを返す
(function template) [編集]
(C++14)
コンテナまたは配列の逆順の終端イテレータを返す
(function template) [編集]
(C++17)
コンテナが空かどうかをチェックする
(function template) [編集]
(C++17)
背後にある配列へのポインタを取得する
(function template) [編集]

[編集] ノート

機能テストマクロ 規格 機能
__cpp_initializer_lists 200806L (C++11) リスト初期化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 が明示的な
特殊化または部分特殊化を持つ可能性がある
そのプログラムは
この場合不適格となる

[編集] 関連項目

(C++20)
連続したオブジェクトのシーケンスに対する所有権を持たないビュー
(class template) [編集]
読み取り専用の文字列ビュー
(class template) [編集]
English 日本語 中文(简体) 中文(繁體)