Home | Projects | Notes > C++ Programming > Generic Programming & Templates

Generic Programming & Templates

 

What is Generic (or Meta) Programming?

"Writing code that works with a variety of types as arguments, as long as those argument types meet specific syntactic and semantic requirements." - Bjarne Stroustrup

Generic programming can be achieved by using:

 

Preprocessor Macros

Example

Generic Programming with Macros

Templates

Generic Programming with Function Templates

Generic Programming with Class Templates

Project: Create a Generic Array Class Template

This is just for practice purposes. Since C++11, the STL includes std::array, a template-based, fixed-size array class. Use std::array instead of raw arrays whenever possible for improved safety, usability, and integration with STL algorithms.

All the array objects in the main function are created on the stack, not on the heap.