Creating Your First Template in C++
Let's see what is happening when we specify different types for the template. For this we will use typeid().name(), it simply shows the data type of the variable. Look at the example below:
main.cpp
1234567#include <iostream> int main() { int x = 5; std::cout << typeid(x).name(); }
Note
The code snippet above will just display in console
i, symbolizing thatxis anint.
Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.
main.cpp
12345678910111213#include <iostream> template<typename Type> void check_type() { std::cout << typeid(Type).name() << std::endl; } int main() { // Try to change `int` to different type check_type<int>(); }
As you can see, when you call the template function and specify a type within the brackets, the typename parameter receives that type. The name of the template parameter essentially becomes an alias for that type.
Swipe to start coding
- Turn the
swapfunction into a template function:- define template using the
templatekeyword; - add the list of template parameters;
- add one parameter for the template.
- define template using the
- Change all types (
int) to thetypenamefrom the template parameter. - Call the template function and pass
charas the type, swapping two variables in themainfunction.
Løsning
solution.cpp
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Opsummér dette kapitel
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 5.88
Creating Your First Template in C++
Stryg for at vise menuen
Let's see what is happening when we specify different types for the template. For this we will use typeid().name(), it simply shows the data type of the variable. Look at the example below:
main.cpp
1234567#include <iostream> int main() { int x = 5; std::cout << typeid(x).name(); }
Note
The code snippet above will just display in console
i, symbolizing thatxis anint.
Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.
main.cpp
12345678910111213#include <iostream> template<typename Type> void check_type() { std::cout << typeid(Type).name() << std::endl; } int main() { // Try to change `int` to different type check_type<int>(); }
As you can see, when you call the template function and specify a type within the brackets, the typename parameter receives that type. The name of the template parameter essentially becomes an alias for that type.
Swipe to start coding
- Turn the
swapfunction into a template function:- define template using the
templatekeyword; - add the list of template parameters;
- add one parameter for the template.
- define template using the
- Change all types (
int) to thetypenamefrom the template parameter. - Call the template function and pass
charas the type, swapping two variables in themainfunction.
Løsning
solution.cpp
Tak for dine kommentarer!
single