Synopsis:
This lecture provides an introduction to C++ Template functions and classes.
We will cover basic ideas in a presentation and then discuss code examples.
Readings and Activities for the Week:
- CPPPL: Chapters 23 - 26, focusing on Chapter 23. CPPSL: Chapter 6
- Look carefully at your second Project assignment: Projects > Project #2
- Look carefully at C++ Properties example code.
Glossary of Terms
-
Function:
A language construct that names a block of processing instructions, passes input arguments for processing, and returns a single argument result. Everytime a function is called, temporary stack memory is allocated for the function to hold its arguments, local data, and return value. When the thread of execution leaves the function, its temporary memory becomes invalid, and, at any time after, may be allocated to another function and be overwritten. -
Class:
A language construct that binds together member functions and data:- Class syntax: class X { ... }
- Method syntax: public void f(T t) { ... }
- Data syntax: private Y y = new Y;
-
Template Function:
A function using an unspecified type parameter, allowing specification with several different specific types without writting a lot of almost duplicate code. Here's an example of template function syntax:template<typename T> void someFunction(T& t) { ... }
-
Template Class:
Classes can be templates and also avoid writing a lot of nearly duplicate code. Here's an example of template class syntax:template<typename T> class SomeClass { ... }
We will discuss this syntax carefully in this lecture.
Lecture Content:
-
Projects:
-
Sample Project #1,
Code folder
Lexical scanner -
Sample Project #2.
Code folder
Rule-based parser - uses templates for ScopeStack -
Sample Project #3,
Code folder
Parser with Abstract Syntax Tree - uses templates for AST -
Sample Project #4,
Code folder
Code Analyzer - uses templates to enable functions to accept callable objects of a variety of types - function pointers, functors, and lambdas
-
Sample Project #1,
Code folder
-
Focus for this Lecture - C++ Templates
Quick overview Templates Presentation, pptx
Templates are one of the terrific features of the C++ language. Almost all of the Standard Template Library (STL) is implemented with templates, and they are very useful in day-to-day programming. fully functional C++ Properties, courtesy of templates
This project demonstrates how easy it is to extend the C++ language. C++ doesn't natively support properties but it's not hard to provide them. First template demo
Provides a simple type to string conversion facility. Template Code Examples
Basic and some more advanced template examples. STL Container examples
This code contains a simple example of the new Variadic Template functionality in the "inserter" project. Survey of STL Container Classes
Simple demos of almost all of the STL containers. STL Template-based Functors and Algorithms
Another set of template code examples, including functors, algorithms, and several other things you need to know. Functor examples Excellent template Q&A
This is a good place to find information to supplement class texts and notes. -
XML:
-
Preparation for Synchronous Discussion:
- Download, build, and run the Parser project.
- Be prepared to describe how this code works. You will find the Parser Blog helpful.
-
Lecture Take-away
The most important things we discussed today are: Next time we will continue with class relationships. Please look at: