Reputation: 1722
I am trying to implement a wrapper on top of std::vector, i am facing an issue with the operator[] method, it works fine for all data types except bool, i know that vector is a specialization. My operator[] function is simple, i just call the vector[index] inside my wrapper, the code wont compile as we cannot return reference of a bool which is an rvalue. I am pasting relevant parts of my class here.
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); }
};
Upvotes: 0
Views: 1599
Reputation: 275415
using reference = T&;
using const_reference = T const&;
these are wrong for vector of bool. bool vectors use pseudo references to permit them to pack the bools into individual bits. As you cannot have a reference to a bit, they did a hack.
Use
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;
Vector of bool is a mess.
Upvotes: 3