The C++ explicit class template instantiation program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Creating and using the C++ explicit class template instantiation in C++ programming

To show: How to create and use the C++ explicit class template instantiation in C++ programming

 

// C++ explicit template instantiation

#include <iostream>

using namespace std;

 

template <class any_data_type>

class Test

{

public:

// a constructor

Test(){ };

// a destructor

~Test(){ };

// a member functions...

any_data_type Funct1(any_data_type Var1)

{return Var1;}

any_data_type Funct2(any_data_type Var2)

{return Var2;}

};

 

// explicit instantiation of class Test<int>

template class Test<int>;

// explicit instantiation of class Test<double>

template class Test<double>;

 

// do some testing

int main(void)

{

Test<int> Var1;

Test<double> Var2;

cout<<"Var1 = "<<Var1.Funct1(200)<<endl;

cout<<"Var2 = "<<Var2.Funct2(3.123)<<endl;

return 0;

}

 

Output examples:

 

Var1 = 200

Var2 = 3.123

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index