Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::initializer_list

      From cppreference.com
      <cpp‎ |utility
       
       
      Utilities library
       
       

      (not to be confused withmember initializer list)

      Defined in header<initializer_list>
      template<class T>
      class initializer_list;
      (since C++11)

      An object of typestd::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of typeconst T (that may be allocated in read-only memory).

      Astd::initializer_list object is automatically constructed when:

      std::initializer_list may be implemented as a pair of pointers or pointer and length. Copying astd::initializer_list does not copy thebacking array of the corresponding initializer list.

      The program is ill-formed if an explicit or partial specialization ofstd::initializer_list is declared.

      Contents

      [edit]Member types

      Name Definition
      value_typeT
      referenceconst T&
      const_referenceconst T&
      size_typestd::size_t
      iteratorconst T*
      const_iteratorconst T*

      [edit]Member functions

      creates an empty initializer list
      (public member function)[edit]
      Capacity
      returns the number of elements in the initializer list
      (public member function)[edit]
      Iterators
      returns a pointer to the first element
      (public member function)[edit]
      returns a pointer to one past the last element
      (public member function)[edit]

      [edit]Non-member functions

      overloadsstd::begin
      (function template)[edit]
      specializesstd::end
      (function template)[edit]
      Free function templates overloaded forstd::initializer_list
      returns a reverse iterator to the beginning of a container or array
      (function template)[edit]
      (C++14)
      returns a reverse end iterator for a container or array
      (function template)[edit]
      (C++17)
      checks whether the container is empty
      (function template)[edit]
      (C++17)
      obtains the pointer to the underlying array
      (function template)[edit]

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_initializer_lists200806L(C++11)List-initialization andstd::initializer_list

      [edit]Example

      Run this code
      #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 workstd::cout<< x<<' ';std::cout<<'\n'; auto al={10,11,12};// special rule for autostd::cout<<"The list bound to auto has size() = "<< al.size()<<'\n';auto la= al;// a shallow-copy of top-level proxy objectassert(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-copyassert(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}

      Output:

      constructed with a 5-element listThe vector now has 8 ints:1 2 3 4 5 6 7 8Range-for over brace-init-list:-1 -2 -3The list bound to auto has size() = 3

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2129C++11std::initializer_list could have explicit
      specializations or partial specializations
      the program is
      ill-formed in this case

      [edit]See also

      (C++20)
      a non-owning view over a contiguous sequence of objects
      (class template)[edit]
      read-only string view
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/initializer_list&oldid=177188"

      [8]ページ先頭

      ©2009-2025 Movatter.jp