Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

while(getline(in, line)) textlines.push(new string(line));

// Pop the lines from the stack and print them: string* s;

while((s = (string*)textlines.pop()) != 0) { cout << *s << endl;

delete s;

}

} ///:~

Explicit instantiation

At times it is useful to explicitly instantiate a template; that is, to tell the compiler to lay down the code for a specific version of that template even though you’re not creating an object at that point. To do this, you reuse the template keyword as follows:

template class Bobbin<thread>; template void sort<char>(char*[]);

Here’s a version of the Sorted.cpp example that explicitly instantiates a template before using it:

//: C03:ExplicitInstantiation.cpp #include "Urand.h"

#include "Sorted.h" #include <iostream> using namespace std;

// Explicit instantiation: template class Sorted<int>;

int main() { Sorted<int> is; Urand<47> rand1;

for(int k = 0; k < 15; k++) is.push_back(rand1());

is.sort();

for(int l = 0; l < is.size(); l++) cout << is[l] << endl;

} ///:~

In this example, the explicit instantiation doesn’t really accomplish anything; the program would work the same without it. Explicit instantiation is only for special cases where extra control is needed.

Chapter 15: Multiple Inheritance

143

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu