templates/abc-f15/prog.cpp - Extended variant - revealing the specialization via a data member. | Lecture 5 - slide 40 : 40 Program 2 |
#include <iostream> #include <string> template <typename S, typename T> class A{ // The primary template class public: int a; S s; T t; A() : a{1}, s{}, t{} {}; }; template<> class A <int, std::string> { // Complete specialization to S = int, T = string public: int a; int s; std::string t; A() : a{2}, s{}, t{}{}; }; template<typename S, typename T> class A <S*, T*> { // Partial Specialization to pointers public: int a; S s; // If S* is double, then S is the type double T t; A() : a{3}, s{}, t{}{}; }; template<typename T> class A <T, T> { // Partial specialization: T and S are the same types public: int a; T s; T t; A() : a{4}, s{}, t{}{}; }; int main(){ A<double,bool> a1; // Use of A<S,T> A<int,std::string> a2; // Use of A<int,string> A<double*,std::string*> a3; // Use of A<T*,S*> A<double,double> a4; // Use of A<T,T> std::cout << a1.a << std::endl; // 1 std::cout << a2.a << std::endl; // 2 std::cout << a3.a << std::endl; // 3 std::cout << a4.a << std::endl; // 4 }