簡體   English   中英

std向量頂部的C ++ 11包裝器類

[英]A C++11 wrapper class on top of std vector

我正在嘗試在std :: vector之上實現包裝器,我正面臨operator []方法的問題,它對除bool以外的所有數據類型都適用,我知道vector是一種專門技術。 我的operator []函數很簡單,我只在包裝器內調用vector [index],由於我們無法返回作為右值的布爾值的引用,因此代碼無法編譯。 我在這里粘貼班級的相關部分。

    template<typename T, class Allocator=std::allocator<T>>
    class customVector
    {
        public:
            static thread_local vecHolder<T> __vholder;
            std::vector<T, Allocator> *internal;
            std::vector<T, Allocator> &_internal = *internal;

  typedef typename std::iterator_traits<std::vector<T, Allocator>>::reference reference;
        typedef typename std::iterator_traits<std::vector<T, Allocator>>::reference const const_reference;
        typedef typename std::iterator_traits<std::vector<T, Allocator>>::value_type value_type;
        typedef typename std::iterator_traits<std::vector<T, Allocator>>::pointer pointer;
        typedef typename std::iterator_traits<std::vector<T, Allocator>>::difference_type difference_type;
        typedef typename std::iterator_traits<std::vector<T, Allocator>>::iterator_category iterator_category;


            using iterator          = T*;
            using const_iterator    = T const*;
            using riterator         = std::reverse_iterator<iterator>;
            using const_riterator   = std::reverse_iterator<const_iterator>;
            using size_type         = std::size_t;

            customVector(int capacity = 8)
                : internal(__fetch_prevec(T, Allocator))
            {}

            customVector(std::initializer_list<T> const& list)
                : internal(__fetch_prevec(T, Allocator))
            {
                std::copy(list.begin(), list.end(), std::back_inserter(_internal));
                return;
            }

            customVector(customVector const& copy)
                : internal(__fetch_prevec(T, Allocator))
            {
                _internal = copy._internal;
                return;
            }

            customVector(customVector&& move) noexcept
                : internal(__fetch_prevec(T, Allocator))
                {
                    _internal = std::move(move._internal);
                    return;
                }

            ~customVector()
            {
                _internal.clear();
                __vholder.put(internal);
                internal = nullptr;
                return;
            }

            inline customVector& operator=(customVector const& copy) { _internal = copy._internal; }
            inline customVector& operator=(customVector&& move) noexcept { _internal = std::move(move._internal); }
            inline void swap(customVector& other) noexcept { std::swap(_internal, other._internal); }
            inline size_type           size() const                        { return _internal.size(); }
            inline bool                empty() const                       { return _internal.empty(); }
            inline reference           at(size_type index)                 { return _internal.at(index); }
            inline const_reference     at(size_type index) const           { return _internal.at(index); }
            inline reference           operator[](size_type index)         { return _internal[index]; }
            inline const_reference     operator[](size_type index) const   { return _internal[index]; }
            inline reference           front()                             { return _internal.front(); }
            inline const_reference     front() const                       { return _internal.front(); }
            inline reference           back()                              { return _internal.back(); }
            inline const_reference     back() const                        { return _internal.back(); }
            inline iterator            begin()                             { return &*_internal.begin(); }
            inline const_iterator      begin() const                       { return &*_internal.begin(); }
            inline iterator            end()                               { return &*_internal.end(); }
            inline const_iterator      end() const                         { return &*_internal.end(); }
            inline const_iterator      cbegin() const                      { return _internal.cbegin(); }
            inline const_iterator      cend() const                        { return _internal.cend(); }
            inline bool operator!=(customVector const& rhs) const          { return !( *this._internal == rhs._internal); }
            inline bool operator==(customVector const& rhs) const { return *this._internal == rhs._internal; }
            inline void push_back(value_type const& value) { _internal.push_back(value); }
            inline void push_back(value_type&& value) { _internal.push_back(std::move(value)); }
            template<typename... Args> inline void emplace_back(Args&&... args) { _internal.emplace_back(std::forward<Args>(args)...); }
            inline void pop_back() { _internal.pop_back(); }
            inline void reserve(size_type capacityUpperBound) { _internal.reserve(capacityUpperBound); }
            inline void resize (size_type n) { _internal.resize(n); }
            inline void resize (size_type n, const value_type& val) { _internal.resize(n, val); }
    };
    using reference         = T&;
    using const_reference   = T const&;

這些對於布爾向量是錯誤的。 布爾向量使用偽引用來允許它們將布爾打包成單個位。 由於您無法參考一點,所以他們做了一個hack。

采用

using reference=typename std::iterator_traits<typename std::vector<T>::iterator>::reference;
using const_reference=typename std::iterator_traits<typename std::vector<T>::const_iterator>::reference;

布爾向量是一團糟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM