Контейнер шаблонов с определением класса, который требует только одного из нескольких параметров шаблона - PullRequest
2 голосов
/ 01 августа 2011

Это немного отличается от вопроса, поставленного чуть ниже этого. Предположим, у меня есть класс контейнера, который имеет два параметра шаблона, первый из которых является типом, а второй - размером контейнера.

Теперь у нас есть несколько контейнеров с другим размером хранилища. По сути, функции контейнера (в любом случае все общедоступные) действительно заботятся только о T; N используется только для выделения локального хранилища (распределитель используется, если N недостаточно).

Я собрал простой пример реализации, который демонстрирует проблему, с которой я столкнулся.

#include <iostream>

template <typename T, size_t N = 10>
class TestArray
{
public:
    T Local[N];

    class Iterator
    {
    public:
        T* Array;
        int Index;      

        Iterator() : Array(NULL), Index(-1) { }
        Iterator(T* _array, int _index) : Array(_array), Index(_index) { }

        bool operator == (const Iterator& _other) const 
        { 
             return _other.Index == Index && _other.Array == Array; 
        }

        bool operator != (const Iterator& _other) const 
        { 
            return !(*this == _other); 
        }

        template <size_t _N>
        Iterator& operator = (const typename TestArray<T, _N>::Iterator &_other)
        {
            Array = _other.Array;
            Index = _other.Index;

            return *this;
        }

        void Next() { ++Index; }
        void Prev() { --Index; }

        T& Get() { return Array[Index]; }
    };

    T& operator [] (const int _index) { return Local[_index]; }

    Iterator Begin() { return Iterator(Local, 0); }
    Iterator End() { return Iterator(Local, N); }

    template <size_t _N>
    void Copy(const TestArray<T, _N> &_other, int _index, int _count)
    {   
        int i;

        for (i = 0; i < _count; i++)
            Local[_index + i] = _other.Local[i];
    }   
};

Это действительно вопрос из двух частей. Первая часть, о которой я писал ранее: Контейнер шаблона с несколькими параметрами шаблона, взаимодействующий с другими контейнерами шаблона с другим параметром шаблона . Для второй части я пытаюсь использовать его следующим образом:

int main() {

    TestArray<int> testArray1;
    TestArray<int, 25> testArray2;

    TestArray<int>::Iterator itr;

    itr = testArray1.Begin();

    for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
    {
        itr.Get() = itr1.Index;
    }

    testArray2.Copy(testArray1, 0, 10);

    for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
    {
        std::cout << itr.Get() << std::endl;
    }

    return 0;
}

Вот ссылка IDEONE: http://ideone.com/GlN54

При компиляции с gcc-4.3.4 я получаю следующее:

prog.cpp: In function ‘int main()’:
prog.cpp:67: error: no match for ‘operator=’ in ‘itr = testArray2.TestArray<T, N>::Begin [with T = int, unsigned int N = 25u]()’
prog.cpp:10: note: candidates are: TestArray<int, 10u>::Iterator& TestArray<int, 10u>::Iterator::operator=(const TestArray<int, 10u>::Iterator&)
prog.cpp:67: error: no match for ‘operator!=’ in ‘itr != testArray2.TestArray<T, N>::End [with T = int, unsigned int N = 25u]()’
prog.cpp:19: note: candidates are: bool TestArray<T, N>::Iterator::operator!=(const TestArray<T, N>::Iterator&) const [with T = int, unsigned int N = 10u]

В VS2010 я получаю следующее:

1>------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=int,
1>              N=25
1>          ]
1>          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(34): could be 'TestArray<T>::Iterator &TestArray<T>::Iterator::operator =(const TestArray<T>::Iterator &)'
1>          with
1>          [
1>              T=int
1>          ]
1>          while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=int,
1>              N=25
1>          ]
1>          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(19): could be 'bool TestArray<T>::Iterator::operator !=(const TestArray<T>::Iterator &) const'
1>          with
1>          [
1>              T=int
1>          ]
1>          while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1>          with
1>          [
1>              T=int
1>          ]

Я думал, что Iterator& operator = сделает так, чтобы этот оператор присваивания работал, но, очевидно, нет. Возможно, я худею, но не могу найти правильное решение здесь. У кого-нибудь есть предложения?

Ответы [ 2 ]

6 голосов
/ 01 августа 2011

TestArray<T, 1> и TestArray<T, 2> являются различными типами , а также TestArray<T, 1>::Iterator и TestArray<T, 2>::Iterator. Назначение не может работать! (Ваш itr отличается от типа testArray2.Begin().)

Вся конструкция кажется мне очень сомнительной - действительно ли это необходимо? Чего ты пытаешься достичь? Вы смотрели на std::array?


Обновление: работает, если вы явно указываете параметры шаблона:

  for (itr.operator=<int,25>(testArray2.Begin());
       itr.operator!=<int,25>(testArray2.End());
       itr.Next())
  {
    std::cout << itr.Get() << std::endl;
  }

Я не совсем уверен, почему параметры не могут быть выведены из аргументов, и я с нетерпением жду хорошего объяснения - тем временем я поставил полный код на Ideone , хотя это не компилируется там, но это происходит с моим GCC 4.6.1.

2 голосов
/ 01 августа 2011

Когда вы используете аргументы шаблона по умолчанию, компилятор просто автоматически предоставляет значения по умолчанию, но они все еще являются частью определения шаблона. В вашем случае TestArray<int> совпадает с TestArray<int, 10>. Если вы хотите, чтобы он взаимодействовал между TestArray<T, N> and TestArray<T, M>, вы, вероятно, захотите использовать функции шаблонов.

Редактировать: Единственное решение, которое мне удалось найти, - это выделить итератор для его собственного класса, и тогда он будет работать как нужно. Я не могу сказать вам, почему это не работает. Я думаю, это потому, что ему не нравится искать внутренние классы для возможных расширений шаблона. В любом случае, вот что я сделал.

#include <iostream>

template <typename T, size_t N = 10> class TestArray;

template<typename T, size_t N> 
    class TIterator
    {
    public:
      T* Array;
      int Index;      

      TIterator() : Array(NULL), Index(-1) { }
      TIterator(T* _array, int _index) : Array(_array), Index(_index) { }

      bool operator == (const TIterator& _other) const 
      { 
    return _other.Index == Index && _other.Array == Array; 
      }

      bool operator != (const TIterator& _other) const 
      { 
    return !(*this == _other); 
      }

      TIterator& operator = (const TIterator& _other) {
    Array = _other.Array;
    Index = _other.Index;
    return *this;
      }

      template <size_t M>
      bool operator == (const TIterator<T, M>& _other) const 
      { 
    return _other.Index == Index && _other.Array == Array; 
      }

      template <size_t M>
      bool operator != (const TIterator<T, M>& _other) const 
      { 
    return !(*this == _other); 
      }

      template< size_t M>
      TIterator& operator = (const TIterator<T, M>& _other) {
    Array = _other.Array;
    Index = _other.Index;
      }

      void Next() { ++Index; }
      void Prev() { --Index; }

      T& Get() { return Array[Index]; }
    };



template <typename T, size_t N>
class TestArray
{
public:
  T Local[N];

  typedef TIterator<T, N> Iterator;

  T& operator [] (const int _index) { return Local[_index]; }

  Iterator Begin() { return Iterator(Local, 0); }
  Iterator End() { return Iterator(Local, N); }

  template <size_t _N>
  void Copy(const TestArray<T, _N> &_other, int _index, int _count)
  {   
    int i;

    for (i = 0; i < _count; i++)
      Local[_index + i] = _other.Local[i];
  }   
};
int main() {

  TestArray<int> testArray1;
  TestArray<int, 25> testArray2;
  TestArray<double, 10> testArray3;

  TestArray<int>::Iterator itr;

  itr = testArray1.Begin();

  for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
    {
      itr.Get() = itr.Index;
    }

  testArray2.Copy(testArray1, 0, 10);


  for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
    {
      std::cout << itr.Get() << std::endl;
    }


  return 0;
}

Обратите внимание, я TIterator использовал оба аргумента типа, хотя технически он не использовал второй аргумент шаблона, просто чтобы показать, что он работал.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...