Q: Can you explain with examples the C++ Template programming model, and how to compile and run a Te
A: Template programming in C++ is a powerful feature that allows you to define generic types and functions that can be used with different data types. This helps support metaprogramming, where you can perform compile-time computations and generate code based on template parameters.
Here's an example to explain the C++ Template programming model:
#include
// A simple template function that adds two values
template
T add(T a, T b) {
return a + b;
}
int main() {
int result1 = add(5, 10);
std::cout << "Result 1: " << result1 << std::endl;
double result2 = add(3.14, 2.71);
std::cout << "Result 2: " << result2 << std::endl;
return 0;
}In this example, we define a template function add() that takes two parameters of the same type T and returns their sum. The template parameter T is a placeholder for any valid type. This allows us to use the function with both int and double types.
To compile and run the template program, you can follow these steps (assuming you have a C++ compiler installed):
Save the code in a file named
template_example.cpp.Open a command prompt or terminal and navigate to the location of the file.
Use a C++ compiler (e.g., g++) to compile the code:
g++ template_example.cpp -o template_exampleRun the compiled program:
./template_example
The output should be:
Result 1: 15
Result 2: 5.85In this example, the template function add() is compiled separately for both int and double types at compile-time. This demonstrates how the C++ Template programming model facilitates metaprogramming by generating code based on template parameters.
Last updated