C++ function templates 


C++ function templates are a powerful feature of the C++ programming language that allows you to write generic functions that can work with multiple data types. 


Function templates are used to define a function that can accept any data type as input and perform a specific operation on it. 


This allows you to write code that is more generic and flexible, and reduces the amount of redundant code you have to write.


Creating a function template in C++ is easy. To define a function template, you start with the keyword "template" followed by the function signature. 


The function signature is the same as any other function, except that you use a placeholder type instead of a specific data type. For example:


cCopy code


template<typename T>

 void print(T value) 

std::cout << value << std::endl;

 } 


In this example, the function template "print" takes a single parameter of type "T" and outputs it to the console. 


The "typename" keyword is used to specify that "T" is a placeholder type.


When you call a function template, you provide the data type you want to use for the placeholder.

 For example, to call the "print" function with a string:


cCopy code


print<std::string>("Hello, world!"); 


The above code will output "Hello, world!" to the console.


You can also use type inference to allow the compiler to deduce the data type automatically. 

For example, you can call the "print" function with an integer without specifying the data type:


scssCopy code


print(42); 


The above code will output "42" to the console. 

The compiler will deduce that the data type of the parameter is "int" based on the value you provided.


Function templates can also take multiple parameters. For example, consider the following function template:


cssCopy code


template<typename T> T max(T a, T b) 

{ return a > b ? a : b; 


In this example, the function template "max" takes two parameters of type "T" and returns the larger value. 

The data type of the parameters and the return value are all the same, which is specified using the placeholder type "T".


To call the "max" function template, you provide two values of the same data type. For example:


sqlCopy code


int result = max(10, 20); 


The above code will set "result" to 20, which is the larger of the two values.


Function templates can also be specialized to handle specific data types. For example, consider the following function template:


cCopy code


template<typename T>

 void print(T value) 

std::cout << value << std::endl; 

template<> void print<std::string>(std::string value) 

std::cout << "String: " << value << std::endl; } 


In this example, the "print" function template is specialized for the "std::string" data type. 

The specialized function template takes a string as input and prefixes it with the text "String: ".


When you call the "print" function template with a string:


cCopy code


print<std::string>("Hello, world!"); 


The above code will output "String: Hello, world!" to the console.


In summary, function templates are a powerful feature of the C++ programming language that allow you to write generic functions that can work with multiple data types. 


By using function templates, you can write more generic and flexible code, and reduce the amount of redundant code you have to write.