欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++ std::initializer_list 實現(xiàn)原理勘誤

今天正在看侯捷《C++ 新標準 C++11-14》的視頻,里面講到 std::initializer_list 的實現(xiàn)原理,并且把源碼貼出來。

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供雙河企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、做網(wǎng)站、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為雙河眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。

  /// initializer_list
  template<class _E>
    class initializer_list
    {
    public:
      typedef _E 		value_type;
      typedef const _E& 	reference;
      typedef const _E& 	const_reference;
      typedef size_t 		size_type;
      typedef const _E* 	iterator;
      typedef const _E* 	const_iterator;

    private:
      iterator			_M_array;
      size_type			_M_len;

      // The compiler can call a private constructor.
      constexpr initializer_list(const_iterator __a, size_type __l)
      : _M_array(__a), _M_len(__l) { }

    public:
      constexpr initializer_list() noexcept
      : _M_array(0), _M_len(0) { }

      // Number of elements.
      constexpr size_type
      size() const noexcept { return _M_len; }

      // First element.
      constexpr const_iterator
      begin() const noexcept { return _M_array; }

      // One past the last element.
      constexpr const_iterator
      end() const noexcept { return begin() + size(); }
    };

他認為,構(gòu)造 std::initializer_list 之前編譯器會先構(gòu)造一個 std::array,然后使用 std::arraybegin()size() 構(gòu)造 std::initializer_list。這種說法有一處錯誤。編譯器不會構(gòu)造 std::array,而是在棧上直接構(gòu)造一個數(shù)組 const T[N]。在棧上構(gòu)造的數(shù)組會像其他變量一樣,在離開作用域時自動析構(gòu),不需要手動管理內(nèi)存。std::array 也是如此,它僅在其基礎(chǔ)之上做了一層包裝,使數(shù)組的行為如同其它容器一樣。所以根本沒必要使用 std::array,直接使用數(shù)組就足夠了。

這個是 cppreference.com 的描述:

The underlying array is a temporary array of type const T[N]

明確地說是普通的 array。

這個是 N3337 的描述:

An object of type initializer_list<E> provides access to an array of objects of type const E.

并沒有說是 std::array。

分享題目:C++ std::initializer_list 實現(xiàn)原理勘誤
本文路徑:http://chinadenli.net/article44/dsogoee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、軟件開發(fā)、網(wǎng)站設(shè)計、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)