Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
319
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

Explicit specification of template functions

Controlling template instantiation

Normally templates are not instantiated until they are needed. For function templates this just means the point at which you call the function, but for class templates it’s more granular than that: each individual member function of the template is not instantiated until the first point of use. This means that only the member functions you actually use will be instantiated, which is quite important since it allows greater freedom in what the template can be used with. For example:

//: C03:DelayedInstantiation.cpp

//Member functions of class templates are not

//instantiated until they're needed.

class X { public:

void f() {}

};

class Y { public:

void g() {}

};

template <typename T> class Z { T t;

public:

void a() { t.f(); } void b() { t.g(); }

};

int main() { Z<X> zx;

zx.a(); // Doesn't create Z<X>::b() Z<Y> zy;

zy.b(); // Doesn't create Z<Y>::a()

Chapter 15: Multiple Inheritance

144

Соседние файлы в предмете Программирование