Contenido del Curso
C++ Templates
C++ Templates
Passing Multiple Arguments with Different Types
In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.
main
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
¿Todo estuvo claro?
Passing Multiple Arguments with Different Types
In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.
main
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
¿Todo estuvo claro?
Passing Multiple Arguments with Different Types
In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.
main
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type
¿Todo estuvo claro?
In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.
main
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }
Tarea
Each argument of a template function has to be a different data type.
- Create enough template parameters for each argument of the function to set a data type