2017-05-21 21 views
0

私はstd :: vectorの上にラッパーを実装しようとしていますが、私は演算子[]メソッドの問題に直面しています。ブール、私はそのベクトルが専門であることを知っています。私の演算子[]関数は単純です、私はちょうど私のラッパーの中でベクトル[インデックス]を呼び出し、私たちはrvalueであるboolの参照を返すことができないので、コードはコンパイルされません。 ここで私のクラスの関連する部分を貼り付けています。標準ベクトルの上にC++ 11ラッパークラス

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

上記のクラスエラーでコンパイルエラーが発生しました:タイプ 'customVector >の非const参照の無効な初期化が無効です::参照' {bool& bool ' –

+3

'ベクトル'の 'operator []'は特別なラッパークラスを返さなければならないので奇妙です。 'bool'へのダイレクトリファレンスではありません('ブール」、ビット参照などはありません)。同じように特化する必要があります。 – ShadowRanger

+3

あなたの質問は何ですか? –

答えて

3
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; 

ブール値のベクトルが混乱です。

+0

提案された変更を追加しましたが、エラーが発生しました。iterator_traitsが参照、ポインタ、および値型などを持っていないようです。 エラー:構造体std :: iterator_traits 、std :: allocator >> ' typedef typename std :: iterator_traits > ::リファレンス参照。 –

+0

上記の必要なところに@ravi 'typename'が追加されました。 – Yakk

+3

待ち時間... 'ベクトル :: reference'の何が問題なのですか? –

関連する問題