![](/img/trans.png)
[英]C++11 std::vector of template class with argument in constructor
[英]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.