Search⌘ K

- Examples

Explore multiple C++ template specialization examples demonstrating partial, full, and external specializations. Understand how to customize templates for different data types to improve code reuse and flexibility. This lesson also covers template type traits and type deduction techniques.

Example 1: template specialization #

C++
// TemplateSpecialization.cpp
#include <iostream>
class Account{
public:
explicit Account(double b): balance(b){}
double getBalance() const {
return balance;
}
private:
double balance;
};
template <typename T, int Line, int Column>
class Matrix{
std::string getName() const { return "Primary Template"; }
};
template <typename T>
class Matrix<T,3,3>{
std::string name{"Partial Specialization"};
};
template <>
class Matrix<int,3,3>{};
template<typename T>
bool isSmaller(T fir, T sec){
return fir < sec;
}
template <>
bool isSmaller<Account>(Account fir, Account sec){
return fir.getBalance() < sec.getBalance();
}
int main(){
std::cout << std::boolalpha << std::endl;
Matrix<double,3,4> primaryM;
Matrix<double,3,3> partialM;
Matrix<int,3,3> fullM;
std::cout << "isSmaller(3,4): " << isSmaller(3,4) << std::endl;
std::cout << "isSmaller(Account(100.0),Account(200.0)): "<< isSmaller(Account(100.0),Account(200.0) ) << std::endl;
std::cout << std::endl;
}

Explanation #

In the example above, we’re modifying the code that we used in the previous lesson.

  • The Primary template is called when we use values other than Matrix<data_type, 3, 4> (line 43).
  • Partial specialization is called when we instantiate Matrix<data_type, 3, 3> where data_type is not an int
...