Contenido del Curso
C++ Templates
C++ Templates
Typename 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.
main
#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
Create a simple template that returns the passed value
- Write a simple template function called
GetValue
. - Use the template's typename parameter as the return type.
- Add a parameter to the template function with the same type as the return type.
- Return the function's parameter.
¡Gracias por tus comentarios!
Typename 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.
main
#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
Create a simple template that returns the passed value
- Write a simple template function called
GetValue
. - Use the template's typename parameter as the return type.
- Add a parameter to the template function with the same type as the return type.
- Return the function's parameter.
¡Gracias por tus comentarios!
Typename 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.
main
#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
Create a simple template that returns the passed value
- Write a simple template function called
GetValue
. - Use the template's typename parameter as the return type.
- Add a parameter to the template function with the same type as the return type.
- Return the function's parameter.
¡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.
main
#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
Create a simple template that returns the passed value
- Write a simple template function called
GetValue
. - Use the template's typename parameter as the return type.
- Add a parameter to the template function with the same type as the return type.
- Return the function's parameter.