Professional Documents
Culture Documents
Why Generic
Programming?
Generic
Programming
Benefits and Basics
Applications Presentation
Function
Type alias,
Alias Template
Function Template Template Assignments
Generic References
Class Template Programming, Codes
Function and
Class
Template
Type Alias,
Alias Template
Class
Template Function
Specialization Template
Class Specialization
Template
Why Generic Programming?
Consider the case of a linked list data structure being developed. Currently if the data item is an
employee information, then this becomes unusable when data changes. Hence there is a need to build a
data type independent linked list data structure which can use any data type at backend. This requires the
data to be treated as void * which can be type casted at runtime and used accordingly. This kind of
programming is known as generic programming.
Generic programming is a style of computer programming in which type is specified as a parameter to a
method, class or interface and the datatype is delayed until instantiation.
Video #1. Generic programming in 2 mins
Benefits
• Code reusability
• Avoid function overloading
• Write once and use many times
• Isolate data from algorithm
• Eliminate complexity and makes it simple to use
Applications
Optional Video #3. C++ Function Templates: How Do They Really Work? - Walter E. Brown
Perfect Forwarding
If a function template forward its arguments without changing its lvalue or rvalue characteristics, then it is known
as Perfect forwarding.
// perfectForwarding1.cpp
#include <iostream>
int main()
{
// Lvalues
int five=5;
int myFive= create<int>(five);
std::cout << "myFive: " << myFive << std::endl;
// Rvalues
int myFive2= create<int>(5);
std::cout << "myFive2: " << myFive2 << std::endl;
}
Benefits of Perfect Forwarding
Class Template
A class using templated data type. Class may consist of the member function and data members of
templated data types. The actual data type is known at the time of instantiation.
Video #4. (animated video and no audio) class with templated functions
private:
int count; //int size
int *data;
};
Type Alias
Type alias is a name that refers to a previously defined type (similar to typedef).
• Type alias is used to overcome the error in typedef due to incorrect order
o typedef int myint //incorrect typedef
• Used to help with legibility and documentation
• Used to write platform independent coding
testScore_t GradeTest();
using testScore_t = int;
// type alias for function pointer
using func = void (*) (int, int);
#ifdef INT_2_BYTES
void example(int, int) {}
using int8_t = char;
using int16_t = int;
int main()
using int32_t = long;
{
#else
Int8_t var = 0x01;
using int8_t = char;
testScore_t x = 90;
using int16_t = short;
using int32_t = int;
func f = example;
#endif
}
Alias template
// alias template
template<class T>
using ptr = T*;
Summary
• Use function and class templates to develop and reuse data independent code algorithms/business
logic.
• For specialized data handling, use function/class templates with specialization.
• Function templates can use default arguments too.
• Instead of typedef use type alias to enhance legibility and documentation
• Use alias template with templates
Reference Codes
Presentation
Assignments
C++ Libraries
C++ Styleguide