Templates
Template Instantiation
-
No automatic type conversion
- Even with types that are convertible (e.g.
intandfloat), multiple functions will still be generated for different types
- Even with types that are convertible (e.g.
-
Implicit template instantiation
func(a, b)
-
Explicit template instantiation
func<type>(a, b)- If a function does not have any argument, we must use explicit template instantiation
Stack vs Heap (p.11)
- Stack variable defined later have a smaller address
- Heap variable defined later have a larger address

Template Specialization
- No formal argument, and specify types of arguments
template <>
If we omit
template <>, it is just a regular function
Class Template
template <typename T>
class ClassName-
Must use explicit template Instantiation
-
Nontype parameters
- Const variables
Pass Array as Reference
void func(int (&arr)[<size>]) {}Separate Compilation
-
Template functions are only instantiated when it is used
-
Definition must be in the same file which calls it
-
Include
.cppfiles within.h, such that it is essentially “in the same file”
Operator Overloading
Rules
- We can only overload existing operators except
.,::,?:,.* - Number of arguments cannot be changed
- Associativity remains unchanged
- Precedence remains unchanged
Operator Precedence/Associativity
| Precedence | Operator | Associativity |
|---|---|---|
| 2 | a++a-- | → |
| 3 | ++a--a | ← |
| 5 | a * ba / ba % b | → |
| 6 | a + ba - b | → |
| 7 | >><< | → |
https://en.cppreference.com/w/cpp/language/operator_precedence
Global vs Member
-
User-defined operator functions can be called with its full name, e.g.
operator+(), but built in operator cannot -
Can be defined as both global or member function
// Global function
Interger operator+(const Intger& a, const Integer& b) {
return Interger(a.getI() + b.getI());
}
// Member function
// Integer i3 = i1 + i2 will be compiled into
// i1.operator+(i2)
Integer operator+(const Integer& a) {
return Interger(i + a.i);
}- Type conversion only happens for parameters
- For member operator function,
1.2 + vecwhich translates into1.2.operator+(vec)does not work - For global operator function, all three cases (
v + v,v + 1.2,1.2 + v)work if there is a conversion constructor
- For member operator function,
Pre/Post-Increment
- ++
- Pre-increment
Vector& operator++();
- Post-increment
Vector operator(int)
- Pre-increment