Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Typename Parameter as a Return Type | Templates Usage
C++ Templates
course content

Contenido del Curso

C++ Templates

C++ Templates

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

bookTypename Parameter as a Return Type

Templates are not just for defining the types of parameters. A powerful feature of templates is that the return type of a function can also depend on the template parameter. This gives you the flexibility to create generic functions with return types that adjust based on the template type provided.

cpp

main

copy
1234567891011121314
#include <iostream> template<typename T> T MyFirstTemplateReturn() { // Returning a default-initialized value of type T return T{}; } int main() { // Call the template with the void type std::cout << typeid(MyFirstTemplateReturn<void>()).name() << std::endl; }

In the example above, the function's return type is defined by the template parameter T. When we specify void as the type, the compiler interprets it accordingly.

Tarea
test

Swipe to show code editor

Create a simple template that returns the passed value

  1. Write a simple template function called GetValue.
  2. Use the template's typename parameter as the return type.
  3. Add a parameter to the template function with the same type as the return type.
  4. Return the function's parameter.

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 2. Capítulo 1
toggle bottom row

bookTypename Parameter as a Return Type

Templates are not just for defining the types of parameters. A powerful feature of templates is that the return type of a function can also depend on the template parameter. This gives you the flexibility to create generic functions with return types that adjust based on the template type provided.

cpp

main

copy
1234567891011121314
#include <iostream> template<typename T> T MyFirstTemplateReturn() { // Returning a default-initialized value of type T return T{}; } int main() { // Call the template with the void type std::cout << typeid(MyFirstTemplateReturn<void>()).name() << std::endl; }

In the example above, the function's return type is defined by the template parameter T. When we specify void as the type, the compiler interprets it accordingly.

Tarea
test

Swipe to show code editor

Create a simple template that returns the passed value

  1. Write a simple template function called GetValue.
  2. Use the template's typename parameter as the return type.
  3. Add a parameter to the template function with the same type as the return type.
  4. Return the function's parameter.

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 2. Capítulo 1
toggle bottom row

bookTypename Parameter as a Return Type

Templates are not just for defining the types of parameters. A powerful feature of templates is that the return type of a function can also depend on the template parameter. This gives you the flexibility to create generic functions with return types that adjust based on the template type provided.

cpp

main

copy
1234567891011121314
#include <iostream> template<typename T> T MyFirstTemplateReturn() { // Returning a default-initialized value of type T return T{}; } int main() { // Call the template with the void type std::cout << typeid(MyFirstTemplateReturn<void>()).name() << std::endl; }

In the example above, the function's return type is defined by the template parameter T. When we specify void as the type, the compiler interprets it accordingly.

Tarea
test

Swipe to show code editor

Create a simple template that returns the passed value

  1. Write a simple template function called GetValue.
  2. Use the template's typename parameter as the return type.
  3. Add a parameter to the template function with the same type as the return type.
  4. Return the function's parameter.

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!

Templates are not just for defining the types of parameters. A powerful feature of templates is that the return type of a function can also depend on the template parameter. This gives you the flexibility to create generic functions with return types that adjust based on the template type provided.

cpp

main

copy
1234567891011121314
#include <iostream> template<typename T> T MyFirstTemplateReturn() { // Returning a default-initialized value of type T return T{}; } int main() { // Call the template with the void type std::cout << typeid(MyFirstTemplateReturn<void>()).name() << std::endl; }

In the example above, the function's return type is defined by the template parameter T. When we specify void as the type, the compiler interprets it accordingly.

Tarea
test

Swipe to show code editor

Create a simple template that returns the passed value

  1. Write a simple template function called GetValue.
  2. Use the template's typename parameter as the return type.
  3. Add a parameter to the template function with the same type as the return type.
  4. Return the function's parameter.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 1
Switch to desktopCambia 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