SlideShare a Scribd company logo
1 of 16
C++: шаблоны, часть 1 Дмитрий Штилерман, Рексофт
Шаблоны:  прагматический подход ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Если бы не шаблоны классов... ,[object Object],[object Object],class Stack  {   void push(void*);   void* pop();  }; void f()  {   Stack s;   s.push((void*) new Cat);   Dog* dog = (Dog*) s.pop();   dog->bark();  }; class StackElem  {/*???*/}; class Stack  {   void push(StackElem*);   StackElem* pop();  }; class Cat : public StackElem  {}; class Dog : public StackElem  {};
Если бы не шаблоны функций... ,[object Object],[object Object],[object Object],#define max(a,b) ((a>b) ? (a) : (b)) const Comparable& max(const Comparable&, const Comparable&); int max(int a, int b) {return (a>b) ? a : b;} short max(short a, short b) {return (a>b) ? a : b;} long max(long a, long b) {return (a>b) ? a : b;} float max(float a, float b) {return (a>b) ? a : b;} double max(double a, double b) {return (a>b) ? a : b;}
Когда использовать шаблоны - самый простой критерий ,[object Object],[object Object],[object Object],[object Object]
Параметры шаблонов ,[object Object],[object Object],[object Object]
Instantiation ,[object Object],[object Object],[object Object],[object Object],[object Object]
Шаблон  как  область действия ,[object Object],[object Object],[object Object]
Specialization ,[object Object],[object Object],[object Object],[object Object],[object Object],template<class T> void MyCopy(T* dst, T* src, int n)  {   if(dst && src && (dst != src)   for(int i = 0; i < n; i++)   dst[i] = src[i];  } template<> void MyCopy<int>(int* dst, int* src, int n)  {memmove(dst, src, n);}
Реализация шаблонов ,[object Object],template<class T> class Stack  {public:   Stack() {}   void push(T) {…}   T pop() {…}  }; void f()  {   Stack<int> is;   Stack<string> ss;  } class Stack_int  {public:   Stack_int() {}   void push(int) {…}   int pop() {…}  }; class Stack_string  {…}; void f()  {   Stack_int   } А что во втором приближении ?  Проблема «code bloating». Template instantiation
Понятие  связывания ( linkage) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Какое  связывание имеет экземпляр шаблона ? ,[object Object],[object Object],[object Object],[object Object],[object Object],// foo.h template<class T> void foo(T* p)  {p->xyz();} // file1.cpp #include  “foo.h” void bar1(X* x)  {foo(x);} // file2.cpp #include  “foo.h” void bar2(X* x)  {foo(x);}
Generic programming ,[object Object],[object Object],[object Object],[object Object]
П аттерны (1) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
П аттерны (2) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Пример: паттерн Observer

More Related Content

What's hot

присяжный Root Conf2009 Beta 1
присяжный Root Conf2009 Beta 1присяжный Root Conf2009 Beta 1
присяжный Root Conf2009 Beta 1Liudmila Li
 
226 Ch
226 Ch226 Ch
226 Chanjaan
 
советы, которые спасут ваш компьютер
советы, которые спасут ваш компьютерсоветы, которые спасут ваш компьютер
советы, которые спасут ваш компьютерanna.korovko
 
классификация вредоносного по
классификация вредоносного поклассификация вредоносного по
классификация вредоносного поanna.korovko
 
Thrift Hl 2
Thrift Hl 2Thrift Hl 2
Thrift Hl 2Ontico
 
Mobile Influence Kyivstar
Mobile Influence KyivstarMobile Influence Kyivstar
Mobile Influence Kyivstarinternetua
 
Lecture4 5 aлгоритм_түүний_шинжчанар
Lecture4 5 aлгоритм_түүний_шинжчанарLecture4 5 aлгоритм_түүний_шинжчанар
Lecture4 5 aлгоритм_түүний_шинжчанарGantur Togtokh
 

What's hot (9)

Grape Trends 2009
Grape Trends 2009Grape Trends 2009
Grape Trends 2009
 
Xrumme
XrummeXrumme
Xrumme
 
присяжный Root Conf2009 Beta 1
присяжный Root Conf2009 Beta 1присяжный Root Conf2009 Beta 1
присяжный Root Conf2009 Beta 1
 
226 Ch
226 Ch226 Ch
226 Ch
 
советы, которые спасут ваш компьютер
советы, которые спасут ваш компьютерсоветы, которые спасут ваш компьютер
советы, которые спасут ваш компьютер
 
классификация вредоносного по
классификация вредоносного поклассификация вредоносного по
классификация вредоносного по
 
Thrift Hl 2
Thrift Hl 2Thrift Hl 2
Thrift Hl 2
 
Mobile Influence Kyivstar
Mobile Influence KyivstarMobile Influence Kyivstar
Mobile Influence Kyivstar
 
Lecture4 5 aлгоритм_түүний_шинжчанар
Lecture4 5 aлгоритм_түүний_шинжчанарLecture4 5 aлгоритм_түүний_шинжчанар
Lecture4 5 aлгоритм_түүний_шинжчанар
 

More from Dmitry Stillermann

Как расти самому, помогая расти другим — практическое применение Management T...
Как расти самому, помогая расти другим — практическое применение Management T...Как расти самому, помогая расти другим — практическое применение Management T...
Как расти самому, помогая расти другим — практическое применение Management T...Dmitry Stillermann
 
OO Design with C++: 4. Design Principles, part 1
OO Design with C++: 4. Design Principles, part 1OO Design with C++: 4. Design Principles, part 1
OO Design with C++: 4. Design Principles, part 1Dmitry Stillermann
 
OO Design with C++: 5. Design Principles, part 2
OO Design with C++: 5. Design Principles, part 2OO Design with C++: 5. Design Principles, part 2
OO Design with C++: 5. Design Principles, part 2Dmitry Stillermann
 
OO Design with C++: 2. Inheritance, part 2
OO Design with C++: 2. Inheritance, part 2OO Design with C++: 2. Inheritance, part 2
OO Design with C++: 2. Inheritance, part 2Dmitry Stillermann
 
OO Design with C++: 1. Inheritance, part 1
OO Design with C++: 1. Inheritance, part 1OO Design with C++: 1. Inheritance, part 1
OO Design with C++: 1. Inheritance, part 1Dmitry Stillermann
 

More from Dmitry Stillermann (7)

DiSC Model in Practice
DiSC Model in PracticeDiSC Model in Practice
DiSC Model in Practice
 
Как расти самому, помогая расти другим — практическое применение Management T...
Как расти самому, помогая расти другим — практическое применение Management T...Как расти самому, помогая расти другим — практическое применение Management T...
Как расти самому, помогая расти другим — практическое применение Management T...
 
OO Design with C++: 4. Design Principles, part 1
OO Design with C++: 4. Design Principles, part 1OO Design with C++: 4. Design Principles, part 1
OO Design with C++: 4. Design Principles, part 1
 
OO Design with C++: 5. Design Principles, part 2
OO Design with C++: 5. Design Principles, part 2OO Design with C++: 5. Design Principles, part 2
OO Design with C++: 5. Design Principles, part 2
 
OO Design with C++: 2. Inheritance, part 2
OO Design with C++: 2. Inheritance, part 2OO Design with C++: 2. Inheritance, part 2
OO Design with C++: 2. Inheritance, part 2
 
OO Design with C++: 1. Inheritance, part 1
OO Design with C++: 1. Inheritance, part 1OO Design with C++: 1. Inheritance, part 1
OO Design with C++: 1. Inheritance, part 1
 
OO Design with C++: 0. Intro
OO Design with C++: 0. IntroOO Design with C++: 0. Intro
OO Design with C++: 0. Intro
 

OO Design with C++: 6. Templates & Patterns

Editor's Notes

  1. Alexander: Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution. As an element in the world, each pattern is a relationship between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain spatial configuration which allows these forces to resolve themselves. As an element of language, a pattern is an instruction, which shows how this spatial configuration can be used, over and over again, to resolve the given system of forces, wherever the context makes it relevant.
  2. Alexander: Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution. As an element in the world, each pattern is a relationship between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain spatial configuration which allows these forces to resolve themselves. As an element of language, a pattern is an instruction, which shows how this spatial configuration can be used, over and over again, to resolve the given system of forces, wherever the context makes it relevant.