我正在尝试在std :: vector之上实现一个包装器,我遇到了operator []方法的问题,它适用于除bool之外的所有数据类型,我知道vector是一个特化。我的operator []函数很简单,我只是在我的包装器中调用vector [index],代码不会编译,因为我们不能返回一个作为rvalue的bool的引用。 我在这里粘贴班上的相关部分。
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); }
};
答案 0 :(得分:3)
using reference = T&;
using const_reference = T const&;
这些对于bool的矢量是错误的。 bool矢量使用伪引用来允许它们将bool打包成单个位。由于你不能引用一点,他们做了一个黑客。
使用
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;
布尔的矢量是一团糟。