Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Using Template Function | Creating First Template
C++ Templates
course content

Contenido del Curso

C++ Templates

C++ Templates

1. Creating First Template
2. Templates Usage
3. Template Specialization
4. Class
5. Template Metaprogramming

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.

cpp

main

copy
123456789101112
#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.

cpp

main

copy
123456789101112131415161718192021
#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.

Tarea

  • 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

Tarea

  • 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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 3
toggle bottom row

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.

cpp

main

copy
123456789101112
#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.

cpp

main

copy
123456789101112131415161718192021
#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.

Tarea

  • 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

Tarea

  • 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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 3
toggle bottom row

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.

cpp

main

copy
123456789101112
#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.

cpp

main

copy
123456789101112131415161718192021
#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.

Tarea

  • 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

Tarea

  • 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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo 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.

cpp

main

copy
123456789101112
#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.

cpp

main

copy
123456789101112131415161718192021
#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.

Tarea

  • 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

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt