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

The "template<>" prefix tells the compiler that what follows is a specialization of a template. The type for the specialization must appear in angle brackets immediately following the function name, as it normally would in an explicitly-specified call. Note that we carefully substitute const char* for T in the explicit specialization. Whenever the original template specifies const T, that const modifies the whole type T. It is the pointer to a const char* that is const. Therefore we must write const char* const in place of const T in the specialization. When the compiler sees a call to min with const char* arguments in the program, it will instantiate our const char* version of min so it can be called. The two calls to min in this program call the same specialization of min.

Explicit specializations tend to be more useful for class templates than for function templates. When you provide a full specialization for a class template, though, you may need to implement all the member functions. This is because you are providing a separate class, and client code may expect the complete interface to be implemented.

The standard library has an explicit specialization for vector when it is used to hold objects of type bool. As you saw earlier in this chapter, the declaration for the primary vector class template is:

template >

class vector {…};

To specialize for objects of type bool, you could declare an explicit specialization as follows:

template <>

class vector< bool, allocator > {…};

Again, this is quickly recognized as a full, explicit specialization because of the template<> prefix and because all the primary template’s parameters are satisfied by the argument list appended to the class name. The purpose for vector is to allow library implementations to save space by packing bits into integers.[58] 

It turns out that vector is a little more flexible than we have described, as seen in the next section.

<p>Partial Specialization</p>

Class templates can also be partially specialized, meaning that at least one of the template parameters is left "open" in some way in the specialization. This is actually what vector does; it specifies the object type (bool), but leaves the allocator type unspecified. Here is the actual declaration of vector:

template

class vector;

You can recognize a partial specialization because non-empty parameter lists appear in angle brackets both after the template keyword (the unspecified parameters) and after the class (the specified arguments). Because of the way vector is defined, a user can provide a custom allocator type, even though the contained type of bool is fixed. In other words, specialization, and partial specialization in particular, constitute a sort of "overloading" for class templates.

<p>Partial ordering of class templates</p>

The rules that determine which template is selected for instantiation are similar to the partial ordering for function templates—the "most specialized" template is selected. An illustration follows. (The string in each f( ) member function below explains the role of each template definition.).

//: C05:PartialOrder2.cpp

// Reveals partial ordering of class templates

#include

using namespace std;

template class C {

public:

  void f() {

    cout << "Primary Template\n";

  }

};

template class C {

public:

  void f() {

    cout << "T == int\n";

  }

};

template class C {

public:

  void f() {

    cout << "U == double\n";

  }

};

template class C {

public:

  void f() {

    cout << "T* used \n";

  }

};

template class C {

public:

  void f() {

    cout << "U* used\n";

  }

};

template class C {

public:

  void f() {

    cout << "T* and U* used\n";

  }

};

template class C {

public:

  void f() {

    cout << "T == U\n";

  }

};

int main() {

  C().f();    // 1: Primary template

  C().f();    // 2: T == int

  C().f(); // 3: U == double

  C().f();  // 4: T == U

  C().f(); // 5: T* used [T is float]

  C().f(); // 6: U* used [U is float]

  C().f();  // 7: T* and U* used [float,int]

  // The following are ambiguous:

//   8: C().f();

//   9: C().f();

//  10: C().f();

//  11: C().f();

//  12: C().f();

} ///:~

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

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

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

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

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

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

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

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

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