Я пытаюсь сделать адаптер для любых коллекций C ++, используя шаблоны.
Мне нужно использовать параметры шаблона шаблона, чтобы я мог использовать адаптер, как:
CollectionAdapter<std::vector<int>> a;
И не как
CollectionAdapter<std::vector<int>,int> a;
Что требует двух параметров шаблона одновременно.
Я написал этот класс:
template <
template <class U> class T
>
class CollectionAdapter {
public:
typedef T<U> ThisCol;
typedef void iterator;
CollectionAdapter() {}
bool add(ThisCol& c,const U& i);
bool remove(ThisCol& c,const U& i);
U& getByIndex(int i);
ThisCol instantiate();
iterator getIterator(ThisCol& c);
};
Однако компилятор Visual Studio выдает мне эту ошибку:
error C2065: 'U' : undeclared identifier
Для этой строки:
typedef T<U> ThisCol;
Что я делаю неправильно?
Я не думаю, что вам нужны параметры шаблона шаблона. Вы можете упростить свой код:
template <class T>
class CollectionAdapter
{
public:
typedef T ThisCol;
typedef typename T::value_type value_type;
//typedef void iterator; // what?? did you mean void*?
typedef void* void_iterator; // but not sure what the use of this is.
// you might need the container's iterator types too
typedef typename T::iterator iterator
typedef typename T::const_iterator const_iteratorCollectionAdapter() {}
bool add(T& c,const value_type& i);
bool remove(T& c,const value_type& i);
value_type& getByIndex(int i);
const value_type& getByIndex(int i) const;
ThisCol instantiate();
iterator getIterator(T& c);
};