libstdc++
initializer_list
Go to the documentation of this file.
00001 // std::initializer_list support -*- C++ -*-
00002 
00003 // Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of GCC.
00006 //
00007 // GCC is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 3, or (at your option)
00010 // any later version.
00011 //
00012 // GCC is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // Under Section 7 of GPL version 3, you are granted additional
00018 // permissions described in the GCC Runtime Library Exception, version
00019 // 3.1, as published by the Free Software Foundation.
00020 
00021 // You should have received a copy of the GNU General Public License and
00022 // a copy of the GCC Runtime Library Exception along with this program;
00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 // <http://www.gnu.org/licenses/>.
00025 
00026 /** @file initializer_list
00027  *  This is a Standard C++ Library header.
00028  */
00029 
00030 #ifndef _INITIALIZER_LIST
00031 #define _INITIALIZER_LIST
00032 
00033 #pragma GCC system_header
00034 
00035 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00036 # include <bits/c++0x_warning.h>
00037 #else // C++0x
00038 
00039 #pragma GCC visibility push(default)
00040 
00041 #include <bits/c++config.h>
00042 
00043 namespace std
00044 {
00045   /// initializer_list
00046   template<class _E>
00047     class initializer_list
00048     {
00049     public:
00050       typedef _E        value_type;
00051       typedef const _E&     reference;
00052       typedef const _E&     const_reference;
00053       typedef size_t        size_type;
00054       typedef const _E*     iterator;
00055       typedef const _E*     const_iterator;
00056 
00057     private:
00058       iterator          _M_array;
00059       size_type         _M_len;
00060 
00061       // The compiler can call a private constructor.
00062       constexpr initializer_list(const_iterator __a, size_type __l)
00063       : _M_array(__a), _M_len(__l) { }
00064 
00065     public:
00066       constexpr initializer_list() noexcept
00067       : _M_array(0), _M_len(0) { }
00068 
00069       // Number of elements.
00070       constexpr size_type
00071       size() const noexcept { return _M_len; }
00072 
00073       // First element.
00074       constexpr const_iterator
00075       begin() const noexcept { return _M_array; }
00076 
00077       // One past the last element.
00078       constexpr const_iterator
00079       end() const noexcept { return begin() + size(); }
00080   };
00081 
00082   /**
00083    *  @brief  Return an iterator pointing to the first element of
00084    *          the initilizer_list.
00085    *  @param  __ils  Initializer list.
00086    */
00087   template<class _Tp>
00088     constexpr const _Tp*
00089     begin(initializer_list<_Tp> __ils) noexcept
00090     { return __ils.begin(); }
00091 
00092   /**
00093    *  @brief  Return an iterator pointing to one past the last element
00094    *          of the initilizer_list.
00095    *  @param  __ils  Initializer list.
00096    */
00097   template<class _Tp>
00098     constexpr const _Tp*
00099     end(initializer_list<_Tp> __ils) noexcept
00100     { return __ils.end(); }
00101 }
00102 
00103 #pragma GCC visibility pop
00104 
00105 #endif // __GXX_EXPERIMENTAL_CXX0X__
00106 
00107 #endif // _INITIALIZER_LIST