Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
template class Bobbin<thread>; template void sort<char>(char*[]);
//: C16:Generate.cpp // Explicit instantiation #include <iostream> #include "Sorted.h" #include "Integer.h" using namespace std; // Explicit instantiation: template class Sorted<Integer>; int main() { Sorted<Integer> is; Urand<47> rand1; for(int k = 0; k < 15; k++) is.add(new Integer(rand1())); for(int l = 0; l < is.count(); l++) cout << *is[l] << endl; } ///:~
//: C16:Special.cpp // Note -- this core dumps at least two compilers // Template specialization // A special sort for char* #include <iostream> #include "Sorted.h" using namespace std; // Specialize the class template: template<> // Essential for specialization class Sorted<char> : public TStash<char> { void bubblesort(); public: int add(char* element) { TStash<char>::add(element); bubblesort(); return 0; // Sort moves the element } }; void Sorted<char>::bubblesort() { for(int i = count(); i > 0; i--) for(int j = 1; j < i; j++) if(strcmp(storage[j], storage[j-1]) < 0) { // Swap the two elements: char* t = storage[j-1]; storage[j-1] = storage[j]; storage[j] = t; } } char* words[] = { "is", "running", "big", "dog", "a", }; const int wsz = sizeof words/sizeof *words; int main() { Sorted<char> sc; for(int k = 0; k < wsz; k++) sc.add(words[k]); for(int l = 0; l < sc.count(); l++) cout << sc[l] << endl; } ///:~
//: C19:TemplateFunctionAddress.cpp {O} // Taking the address of a function generated // from a template. template <typename T> void f(T*) {} void h(void (*pf)(int*)) {} template <class T> void i(void (*pf)(T*)) {} int main() { // Full type exposition: h(&f<int>); // Type induction: h(&f); // Full type exposition: i<int>(&f<int>); // Type inductions: i(&f<int>); i<int>(&f); } ///:~Type induction in template functions
As a simple but very useful example, consider the following://: :arraySize.h // Uses template type induction to discover // the size of an array #ifndef ARRAYSIZE_H #define ARRAYSIZE_H template<typename T, int size> int asz(T (&)[size]) { return size; }
//: C19:ArraySize.cpp // The return value of the template function // asz() is a compile-time constant #include <iostream> #include "../arraySize.h" using namespace std; int main() { int a[12], b[20]; const int sz1 = asz(a); const int sz2 = asz(b); int c[sz1], d[sz2]; } ///:~