Because C++ is a strongly typed language, every function must be
declared with a specific types, e.g. int
,
float
, double
, etc.
It can be repetitive to write out one function that accepts multiple types. For example, in Getting to Know C++ vignette, we specified two separate functions for adding integers and doubles:
int add(int x, int y) {
x = 1;
y = 2;
return x + y;
}
double add(double x, double y) {
x = 1.2;
y = 2.4;
return x + y;
}
A separate function is needed for each type we want to be able to return. This can get tedious and repetitive, and can also lead to more code to maintain and more potential for bugs. To simplify matters, C++ allows for templated code, which allows for a generic type.
template <typename A>
template <class A>
//FIMS uses Type as the typename or class:
template <class Type>
Type my_add(Type x, Type y){
return x + y
}
Now when we run the program, we can use the function,
my_add
with different data types:
#include <iostream>
template <typename Type>
Type my_add(Type x, Type y){
return x + y;
}
int main(){
//works with integers
std::cout << "Type is int: " << my_add(1,2) << std::endl;
//works with floats
std::cout << "Type is float: " << my_add(1.2, 3.2) << std::endl;
//works with doubles
std::cout << "Type is double: " << my_add(1.52757396774, 6.83480375227) << std::endl;
return 0;
}
Rcpp does not support C++ templating so this example can be run from your command prompt.
src/my_templated_add.cpp
)