C++ A C++;11 std向量上的包装类

C++ A C++;11 std向量上的包装类,c++,c++11,stl,C++,C++11,Stl,我试图在std::vector之上实现一个包装器,我面临着运算符[]方法的问题,它适用于除bool之外的所有数据类型,我知道vector是一种专门化。我的运算符[]函数很简单,我只调用包装器中的向量[index],代码不会编译,因为我们无法返回布尔值的引用,而布尔值是右值。 我在这里粘贴我班级的相关部分 template<typename T, class Allocator=std::allocator<T>> class customVector

我试图在std::vector之上实现一个包装器,我面临着运算符[]方法的问题,它适用于除bool之外的所有数据类型,我知道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); }
    };
模板
类自定义向量
{
公众:
静态线程\本地向量保持器\ vholder;
标准::向量*内部;
标准::向量和内部=*内部;
typedef typename std::iterator_traits::reference;
typedef typename std::iterator_traits::reference const_reference;
typedef typename std::迭代器特征::值类型值类型;
typedef typename std::迭代器_traits::指针;
typedef typename std::迭代器特征::差异类型差异类型;
typedef typename std::iterator_traits::iterator_category iterator_category;
使用迭代器=T*;
使用常量迭代器=T常量*;
使用riterator=std::reverse_迭代器;
使用const_riterator=std::reverse_迭代器;
使用size\u type=std::size\u t;
customVector(整数容量=8)
:内部(uuu fetch_uprevec(T,分配器))
{}
customVector(标准::初始值设定项\u列表常量和列表)
:内部(uuu fetch_uprevec(T,分配器))
{
std::copy(list.begin()、list.end()、std::back_inserter(_internal));
返回;
}
customVector(customVector常量和副本)
:内部(uuu fetch_uprevec(T,分配器))
{
_内部=复制;
返回;
}
customVector(customVector&&move)无例外
:内部(uuu fetch_uprevec(T,分配器))
{
_内部=标准::移动(移动._内部);
返回;
}
~customVector()
{
_内部清除();
__vholder.put(内部);
内部=空PTR;
返回;
}
内联customVector&运算符=(customVector const©){u internal=copy.\u internal;}
内联customVector&运算符=(customVector&&move)noexcept{u internal=std::move(move.\u internal);}
内联无效交换(customVector和其他)noexcept{std::swap(_internal,other._internal);}
内联大小_type size()常量{return_internal.size();}
内联bool empty()常量{return _internal.empty();}
在(size_type index){return_internal.at(index);}处的内联引用
内联const_引用at(size_类型索引)const{return _internal.at(index);}
内联引用运算符[](size_类型索引){return_internal[index];}
内联常量引用运算符[](大小类型索引)常量{return\u internal[index];}
内联引用front(){return_internal.front();}
内联const_reference front()const{return_internal.front();}
内联引用back(){return\u internal.back();}
inline const_reference back()const{return_internal.back();}
内联迭代器begin(){return&*_internal.begin();}
内联常量迭代器begin()常量{return&*_internal.begin();}
内联迭代器end(){return&*_internal.end();}
内联常量迭代器end()常量{return&*_internal.end();}
内联常量迭代器cbegin()常量{return _internal.cbegin();}
内联常量迭代器cend()常量{return _internal.cend();}
内联布尔运算符!=(customVector const&rhs)const{return!(*this.\u internal==rhs.\u internal);}
内联布尔运算符==(customVector const&rhs)const{return*this.\u internal==rhs.\u internal;}
内联void push_back(value_type const&value){{u internal.push_back(value);}
内联void push_back(值、类型和值){{u internal.push_back(std::move(值));}
模板内联空放置回(Args&…Args){{u internal.emplace回(std::forward(Args)…);}
内联void pop_back(){_internal.pop_back();}
内联无效保留(大小_类型capacityUpperBound){_内部.reserve(capacityUpperBound);}
内联空调整大小(大小\类型n){{u内部.调整大小(n);}
内联void resize(size_type n,const value_type&val){{u internal.resize(n,val);}
};
这些对于布尔向量是错误的。bool向量使用伪引用,允许它们将bool打包为单个位。由于你一点也不知道,他们做了一次黑客攻击

使用

使用reference=typename std::iterator\u traits::reference;
使用const_reference=typename std::iterator_traits::reference;
布尔向量是一团乱

这些对于布尔向量是错误的。bool向量使用伪引用,允许它们将bool打包为单个位。由于你一点也不知道,他们做了一次黑客攻击

使用

使用reference=typename std::iterator\u traits::reference;
使用const_reference=typename std::iterator_traits::reference;
布尔向量是一团乱

    using reference         = T&;
    using const_reference   = T const&;
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;