Читаем Thinking In C++. Volume 2: Practical Programming полностью

The third type of parameter a template can accept is another class template. This may sound strange, since templates are types, and type parameters are already allowed, but if you are going to use a template type parameter as a template in your code, the compiler needs to know that the parameter is a template in the first place. The following example illustrates a template template parameter.

//: C05:TempTemp.cpp

// Illustrates a template template parameter

#include

#include

using namespace std;

template

class Array { // A simple, expandable sequence

  enum {INIT = 10};

  T *data;

  size_t capacity;

  size_t count;

public:

  Array() {

    count = 0;

    data = new T[capacity = INIT];

  }

  void push_back(const T& t) {

    if(count == capacity) {

      // Grow underlying array

      size_t newCap = 2*capacity;

      T* newData = new T[newCap];

      for (size_t i = 0; i < count; ++i)

        newData[i] = data[i];

      delete data;

      data = newData;

      capacity = newCap;

    }

    data[count++] = t;

  }

  void pop_back() {

    if(count > 0)

      --count;

  }

  T* begin() {

    return data;

  }

  T* end() {

    return data + count;

  }

};

template class Seq>

class Container {

  Seq seq;

public:

  void append(const T& t) {

    seq.push_back(t);

  }

  T* begin() {

    return seq.begin();

  }

  T* end() {

    return seq.end();

  }

};

int main() {

  Container theData;

  theData.append(1);

  theData.append(2);

  int* p = theData.begin();

  while(p != theData.end())

    cout << *p++ << endl;

} ///:~

The Array class template is a trivial sequence container. The Container template takes two parameters: the type of the objects it is to hold, and a sequence data structure to do the holding. The following line in the implementation of the Container class requires that we inform the compiler that Seq is a template:.

  Seq seq;

If we hadn’t declared Seq to be a template template parameter, the compiler would complain here that Seq is not a template, since we’re using it as such. In main( ) a Container is instantiated to use an Array to hold integers, so Seq stands for Array in this example.

Note that it is not necessary in this case to name the parameter for Seq inside Container’s declaration. The line in question is:

template class Seq>

Although we could have written

template class Seq>

the parameter U is not needed anywhere. All that matters is that Seq is a class template that takes a single type parameter. This is analogous to omitting the names of function parameters when they’re not needed, such as when you overload the post-increment operator:.

T operator++(int);

The int here is merely a placeholder and therefore needs no name.

The following program uses a fixed-size array, which has an extra template parameter representing the array dimension:

//: C05:TempTemp2.cpp

// A multi-variate template template parameter

#include

#include

using namespace std;

template

class Array {

  T data[N];

  size_t count;

public:

  Array() { count = 0; }

  void push_back(const T& t) {

    if(count < N)

      data[count++] = t;

  }

  void pop_back() {

    if(count > 0)

      --count;

  }

  T* begin() { return data; }

  T* end() { return data + count; }

};

template class Seq>

class Container {

  Seq seq;

public:

  void append(const T& t) { seq.push_back(t); }

  T* begin() { return seq.begin(); }

  T* end() { return seq.end(); }

};

int main() {

  const size_t N = 10;

  Container theData;

  theData.append(1);

  theData.append(2);

  int* p = theData.begin();

  while(p != theData.end())

    cout << *p++ << endl;

} ///:~

Once again, parameter names are not needed in the declaration of Seq inside Container’s declaration, but we need two parameters to declare the data member seq, hence the appearance of the non-type parameter N at the top level.

Combining default arguments with template template parameters is slightly more problematic. The When the compiler looks at the inner parameters of a template template parameter, default arguments are not considered, so you have to repeat the defaults in order to get an exact match. The following example uses a default argument for the fixed-size Array template and shows how to accommodate this quirk in the language.

//: C05:TempTemp3.cpp

//{-bor}

//{-msc}

// Combining template template parameters and

// default arguments

#include

#include

using namespace std;

template  // A default argument

class Array {

  T data[N];

  size_t count;

public:

  Array() { count = 0; }

  void push_back(const T& t) {

    if(count < N)

      data[count++] = t;

  }

  void pop_back() {

    if(count > 0)

      --count;

  }

  T* begin() { return data; }

  T* end() { return data + count; }

};

template class Seq>

class Container {

  Seq seq;  // Default used

public:

  void append(const T& t) { seq.push_back(t); }

  T* begin() { return seq.begin(); }

  T* end() { return seq.end(); }

};

int main() {

  Container theData;

  theData.append(1);

  theData.append(2);

  int* p = theData.begin();

  while(p != theData.end())

    cout << *p++ << endl;

} ///:~

It is necessary to include the default dimension of 10 in the line:

template class Seq>

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных