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

Contenido del Curso

C++ Templates

C++ Templates

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

bookCreating Your First Template

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:

cpp

main

copy
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 that x is an int.

Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.

cpp

main

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

Tarea

  1. Turn the swap function into a template function:
    1. define template using the template keyword;
    2. add the list of template parameters;
    3. add one parameter for the template.
  2. Change all types (int) to the typename from the template parameter.
  3. Call the template function and pass char as the type, swapping two variables in the main function.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookCreating Your First Template

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:

cpp

main

copy
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 that x is an int.

Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.

cpp

main

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

Tarea

  1. Turn the swap function into a template function:
    1. define template using the template keyword;
    2. add the list of template parameters;
    3. add one parameter for the template.
  2. Change all types (int) to the typename from the template parameter.
  3. Call the template function and pass char as the type, swapping two variables in the main function.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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

bookCreating Your First Template

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:

cpp

main

copy
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 that x is an int.

Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.

cpp

main

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

Tarea

  1. Turn the swap function into a template function:
    1. define template using the template keyword;
    2. add the list of template parameters;
    3. add one parameter for the template.
  2. Change all types (int) to the typename from the template parameter.
  3. Call the template function and pass char as the type, swapping two variables in the main function.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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:

cpp

main

copy
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 that x is an int.

Let's create a template using <typename Type> and pass Type as a parameter to the function instead of specifying a specific variable.

cpp

main

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

Tarea

  1. Turn the swap function into a template function:
    1. define template using the template keyword;
    2. add the list of template parameters;
    3. add one parameter for the template.
  2. Change all types (int) to the typename from the template parameter.
  3. Call the template function and pass char as the type, swapping two variables in the main function.

Once you've completed this task, click the button below the code to check your solution.

Switch to desktopCambia 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
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt