0

STL イテレータの 1 つの上にカスタム イテレータを構築したいと考えています。これは、コンパイル可能であると予想されるコードの簡素化されたバージョンです。

#include <cstddef>
#include <iterator>
#include <list>

class Data
{
public:
  double x;
};

typedef std::list<Data> list_of_data;

template <class IteratorType>
class my_iterator :
     public std::iterator<std::bidirectional_iterator_tag,
                          typename IteratorType::value_type >
{
public:
  //type of itself
  typedef my_iterator self;
  //type of underlying iterator
  typedef IteratorType list_iterator_type;

  my_iterator(list_iterator_type it) :
    m_it(it)
  {}//constructor.

  my_iterator(const self& source) :
    m_it(source.m_it)
  {}

  self& operator=(const self& source)
  {
    m_it = source.m_it;
    return *this;
  }//operator=

  bool operator==(self other)
  {
    return m_it == other.m_it;
  }

  bool operator!=(self other)
  {
    return m_it != other.m_it;
  }

  inline typename self::reference operator*()
  { return (*m_it);}

  inline const typename self::reference operator*() const
    { return (*m_it); }

  inline typename self::pointer operator->()
    { return &(*m_it); }

  inline const typename self::pointer operator->() const
    { return &(*m_it); }

  inline self& operator++()
  {
    ++m_it;
  }//operator++

  inline self operator++(int)
  {
    self tmp(*this);
    ++(*this);
    return tmp;
  }//operator++(int)

private:

  list_iterator_type m_it;
};

///non constant iterator over cells.
typedef my_iterator<list_of_data::iterator> iterator;
///constant iterator over cells.
typedef my_iterator<list_of_data::const_iterator> const_iterator;

int main()
{
  list_of_data test_list;
  Data a;
  test_list.push_back(a);
  test_list.push_back(a);
  test_list.push_back(a);
  for(const_iterator it  = const_iterator(test_list.begin()); 
                     it != const_iterator(test_list.end()); ++it)
  {
    double x = it->x;
    double y = (*it).x;
  }
}

ただし、次のエラー メッセージが表示されます。

test_list.cpp: In instantiation of ‘typename my_iterator<IteratorType>::self::pointer my_iterator<IteratorType>::operator->() [with IteratorType = std::_List_const_iterator<Data>; typename my_iterator<IteratorType>::self::pointer = Data*]’:
test_list.cpp:92:18:   required from here
test_list.cpp:55:21: error: invalid conversion from ‘const Data*’ to ‘std::iterator<std::bidirectional_iterator_tag, Data, long int, Data*, Data&>::pointer {aka Data*}’ [-fpermissive]
     { return &(*m_it); }
                     ^
test_list.cpp: In instantiation of ‘typename my_iterator<IteratorType>::self::reference my_iterator<IteratorType>::operator*() [with IteratorType = std::_List_const_iterator<Data>; typename my_iterator<IteratorType>::self::reference = Data&]’:
test_list.cpp:93:18:   required from here
test_list.cpp:49:18: error: invalid initialization of reference of type ‘std::iterator<std::bidirectional_iterator_tag, Data, long int, Data*, Data&>::reference {aka Data&}’ from expression of type ‘const Data’
   { return (*m_it);}

私が見る限り、アクセス演算子の非 const バージョンを参照していますが、ここで定数イテレータを明示的に使用すると、なぜですか?

4

2 に答える 2

0

いくつかの調査の後、最終的に問題を発見したので、上記のコードの正しい、コンパイル可能なバージョンを投稿しています。ハイライトは次のとおりです。

-- 主な問題は、基本 std::iterator クラスのテンプレート パラメーターを十分に定義していなかったことです。5 つのパラメーターが必要ですが、最初の 2 つだけを定義し、残りは既定の型割り当てに依存しています。これは間違いでした。たとえば、デフォルトpointerでは として定義されていますvalue_type*が、実際にconst value_type*は定数イテレータが必要でした。したがって、5 つのテンプレート パラメータをすべて定義しました。

operator*()--との 2 つのバージョンを用意する必要はありませんでしoperator->()た。

-- 非定数イテレータを定数イテレータに割り当てることができるように、別のテンプレート化されたコンストラクタも追加しました。

以下は私の努力の結果です。

#include <cstddef>
#include <iostream>
#include <iterator>
#include <list>

class Data
{
public:
  double x;
};

typedef std::list<Data> list_of_data;

template <class IteratorType>
class my_iterator :
     public std::iterator<std::bidirectional_iterator_tag,
                          typename IteratorType::value_type,
                          typename IteratorType::difference_type,
                          typename IteratorType::pointer,
                          typename IteratorType::reference>
{
public:
  //type of itself
  typedef my_iterator self;
  //type of iterator over cells
  typedef IteratorType list_iterator_type;

  my_iterator(list_iterator_type it) :
    m_it(it)
  {}//constructor.

  my_iterator(const self& source) :
    m_it(source.m_it)
  {}

  template<class another_iterator>
  my_iterator(const my_iterator<another_iterator>& source) :
    m_it(source.m_it)
  {}

  self& operator=(const self& source)
  {
    m_it = source.m_it;
    return *this;
  }//operator=

  bool operator==(self other) const
  {
    return m_it == other.m_it;
  }

  bool operator!=(self other) const
  {
    return m_it != other.m_it;
  }

  inline typename self::reference operator*() const
  { return (*m_it);}

  inline typename self::pointer operator->() const
    { return &(*m_it); }

  inline self& operator++()
  {
    ++m_it;
    return (*this);
  }//operator++

  inline self operator++(int)
  {
    self tmp(*this);
    ++(*this);
    return tmp;
  }//operator++(int)

private:

  list_iterator_type m_it;
};

///non constant iterator over cells.
typedef my_iterator<list_of_data::iterator> iterator;
///constant iterator over cells.
typedef my_iterator<list_of_data::const_iterator> const_iterator;

int main()
{
  list_of_data test_list;
  Data a;
  test_list.push_back(a);
  test_list.push_back(a);
  test_list.push_back(a);
  for(iterator it  = iterator(test_list.begin()); 
                     it != iterator(test_list.end()); ++it)
  {
    it->x = 2;
  }
  for(const_iterator it  = const_iterator(test_list.begin()); 
                     it != const_iterator(test_list.end()); ++it)
  {
    std::cout << "  it->x =" << it->x << std::endl;
    std::cout << "(*it).x =" << (*it).x << std::endl;
  }
}
于 2016-02-11T09:35:58.850 に答える
0

const_iterator itfor ループで使用する場合it、型ですconst_iteratorが、それ自体は非 const です。

したがって、メンバー関数を使用すると非 const メンバーが使用されますが、これは がmy_iteratorreal を格納している場合には機能しませんlist::const_iterator。式&(*m_it)では、によって参照される要素*m_itは const であるため、そのオブジェクトへの非 const アドレスを取得することはできません。

エラー: 'const Data*' からの無効な変換</p>

于 2016-02-10T15:03:47.100 に答える