Conteúdo do Curso
C++ Templates
C++ Templates
Using Template Function
Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.
main
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }
As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template
, comes into play.
main
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }
Note
It doesn't matter what type you specify for this example, so you can change
void
to any type you want. However, specifying the type is mandatory.
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tudo estava claro?
Using Template Function
Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.
main
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }
As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template
, comes into play.
main
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }
Note
It doesn't matter what type you specify for this example, so you can change
void
to any type you want. However, specifying the type is mandatory.
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tudo estava claro?
Using Template Function
Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.
main
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }
As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template
, comes into play.
main
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }
Note
It doesn't matter what type you specify for this example, so you can change
void
to any type you want. However, specifying the type is mandatory.
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type
Tudo estava claro?
Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.
main
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }
As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template
, comes into play.
main
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }
Note
It doesn't matter what type you specify for this example, so you can change
void
to any type you want. However, specifying the type is mandatory.
Tarefa
- Specify the name for typename
- Pass the typename as a parameter for
typeid()
- Call the function template three times specifying the next data types in the
< >
brackets:<int>
data type<float>
data type<double>
data type