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

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

We mentioned earlier that an ordinary function overload of min( ) is preferable to using the template. If a function already exists to match a function call, why generate another? In the absence of ordinary functions, however, it is possible that overloaded function templates can lead to ambiguities. To minimize the chances of this, an ordering is defined for function templates that chooses the most specialized template, if such exists. A function template is considered more specialized than another if every possible list of arguments that matches it also matches the other, but not the other way around. Consider the following function template declarations, taken from an example in the C++ standard document:.

template void f(T);

template void f(T*);

template void f(const T*);

The first template can be matched with any type. The second template is more specialized than the first because only pointer types match it. In other words, you can look upon the set of possible calls that match the second template as a subset of the first. A similar relationship exists between the second and third template declarations above: the third can only be called for pointers to const, but the second accommodates any pointer type. The following program illustrates these rules.

//: C05:PartialOrder.cpp

// Reveals Ordering of Function Templates

#include

using namespace std;

template

void f(T) {

  cout << "T\n";

}

template

void f(T*) {

  cout << "T*\n";

}

template

void f(const T*) {

  cout << "const T*\n";

}

int main() {

  f(0);            // T

  int i = 0;

  f(&i);           // T*

  const int j = 0;

  f(&j);           // const T*

} ///:~

The call f(&i) certainly matches the first template, but since the second is more specialized, it is called. The third can’t be called in this case since the pointer is not a pointer to const. The call f(&j) matches all three templates (for example, T would be const int in the second template), but again, the third template is more specialized, so it is used instead.

If there is no "most specialized" template among a set of overloaded function templates, an ambiguity remains and the compiler will report an error. That is why this feature is called a "partial ordering"—it may not be able to resolve all possibilities. Similar rules exist for class templates (see the section "Partial specialization" below).

<p>Template specialization</p>

The term specialization has a specific, template-related meaning in C++. A template definition is, by its very nature, a generalization, because it describes a family of functions or classes in general terms. When template arguments are supplied, the result is a specialization of the template, because it fixes a unique instance out of the many possible instances of the family of functions or classes. The min function template seen at the beginning of this chapter is a generalization of a minimum-finding function, because the type of its parameters is not specified. When you supply the type for the template parameter, whether explicitly or implicitly via argument deduction, the resultant code generated by the compiler (for example, min) is a specialization of the template. The code generated is also considered an instantiation of the template, of course, as are all code bodies generated by the template facility.

<p>Explicit specialization</p>

You can also provide the code yourself for a given template specialization, should the need arise. Providing your own template specializations is often needed with class templates, but we will begin with the min function template to introduce the syntax.

Recall that in MinTest.cpp earlier in this chapter we introduced the following ordinary function:

const char* min(const char* a, const char* b) {

  return (strcmp(a, b) < 0) ? a : b;

}

This was so that a call to min would compare strings and not addresses. Although it would provide no advantage in this case, we could define instead a const char* specialization for min, as in the following program:.

//: C05:MinTest2.cpp

#include

#include

using std::strcmp;

using std::cout;

using std::endl;

template const T& min(const T& a, const T& b) {

  return (a < b) ? a : b;

}

// An explicit specialization of the min template

template<>

const char* const& min(const char* const& a,

                     const char* const& b) {

  return (strcmp(a, b) < 0) ? a : b;

}

int main() {

  const char *s2 = "say \"Ni-!\"", *s1 = "knights who";

  cout << min(s1, s2) << endl;

  cout << min<>(s1, s2) << endl;

} ///:~

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

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

3ds Max 2008
3ds Max 2008

Одни уверены, что нет лучшего способа обучения 3ds Мах, чем прочитать хорошую книгу. Другие склоняются к тому, что эффективнее учиться у преподавателя, который показывает, что и как нужно делать. Данное издание объединяет оба подхода. Его цель – сделать освоение 3ds Мах 2008 максимально быстрым и результативным. Часто после изучения книги у читателя возникают вопросы, почему не получился тот или иной пример. Видеокурс – это гарантия, что такие вопросы не возникнут: ведь автор не только рассказывает, но и показывает, как нужно работать в 3ds Мах.В отличие от большинства интерактивных курсов, где работа в 3ds Мах иллюстрируется на кубиках-шариках, данный видеокурс полностью практический. Все приемы работы с инструментами 3ds Мах 2008 показаны на конкретных примерах, благодаря чему после просмотра курса читатель сможет самостоятельно выполнять даже сложные проекты.

Владимир Антонович Верстак , Владимир Верстак

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