-
We will look at passing multiple template parameters in our template parameter list.
Have a look at the code:
#include<iostream> template<typename t1, typename t2> void foo(t1 input1, t2 input2) { std::cout<<input1<<std::endl; std::cout<<input2<<std::endl; } int main() { foo<int, float>(3, 4.5f); return 0; }
Output
3
4.5
We can copy this code in https://cppinsights.io/ and have a look at what kind of code compiler generates on our behalf if we use templates.
Following are insights from above webpage:
#include<iostream> template<typename t1, typename t2> void foo(t1 input1, t2 input2) { (std::cout << input1) << std::endl; (std::cout << input2) << std::endl; } /* First instantiated from: insights.cpp:12 */ #ifdef INSIGHTS_USE_TEMPLATE template<> void foo<int, float>(int input1, float input2) { std::cout.operator<<(input1).operator<<(std::endl); std::cout.operator<<(input2).operator<<(std::endl); } #endif int main() { foo<int, float>(3, 4.5F); return 0; }
-
To print the type names we can include <typeinfo>
#include<iostream> #include<typeinfo> template<typename t1, typename t2> void foo(t1 input1, t2 input2) { std::cout<<typeid(input1).name()<<std::endl; std::cout<<typeid(input2).name()<<std::endl; } int main() { foo<int, float>(3, 4.5f); foo<float,int>(3.4f, 6); //this will create two instantiatons of templates. return 0; }