Contenido del Curso
C++ Templates
C++ Templates
Non-type Template Parameters
When defining a template, we can not only specify types but also pass values to those types. This means we can create templates that behave differently based on the values provided but we will return to it a little bit later.
main
#include <iostream> // Non-type parameter means we won't use typename // Instead you can use an actual type for it template<int N> void PrintValue() { std::cout << N << std::endl; } int main() { // Call the template function with a literal integer PrintValue<5>(); // 5 }
The main advantage of this approach is that all calculations are performed at compile time, which enables its use in metaprogramming.
main
#include <iostream> // Template function to calculate the square of a non-type parameter I template<int I> int Square() { return I * I; } // Return the square of I int main() { // The result of Square<5>() is computed at compile time int b = Square<5>(); // b will be initialized to 25 }
When you run the compiled program, the value of b
will already be 25
. The calculation is performed at compile time, resulting in zero runtime operations.
Swipe to show code editor
Complete the template function and find the sum of options and operations variables.
- Complete the template function.
- Change the type of the
options
andoperations
variables to make it possible to pass them as a template parameters.
¡Gracias por tus comentarios!
Non-type Template Parameters
When defining a template, we can not only specify types but also pass values to those types. This means we can create templates that behave differently based on the values provided but we will return to it a little bit later.
main
#include <iostream> // Non-type parameter means we won't use typename // Instead you can use an actual type for it template<int N> void PrintValue() { std::cout << N << std::endl; } int main() { // Call the template function with a literal integer PrintValue<5>(); // 5 }
The main advantage of this approach is that all calculations are performed at compile time, which enables its use in metaprogramming.
main
#include <iostream> // Template function to calculate the square of a non-type parameter I template<int I> int Square() { return I * I; } // Return the square of I int main() { // The result of Square<5>() is computed at compile time int b = Square<5>(); // b will be initialized to 25 }
When you run the compiled program, the value of b
will already be 25
. The calculation is performed at compile time, resulting in zero runtime operations.
Swipe to show code editor
Complete the template function and find the sum of options and operations variables.
- Complete the template function.
- Change the type of the
options
andoperations
variables to make it possible to pass them as a template parameters.
¡Gracias por tus comentarios!
Non-type Template Parameters
When defining a template, we can not only specify types but also pass values to those types. This means we can create templates that behave differently based on the values provided but we will return to it a little bit later.
main
#include <iostream> // Non-type parameter means we won't use typename // Instead you can use an actual type for it template<int N> void PrintValue() { std::cout << N << std::endl; } int main() { // Call the template function with a literal integer PrintValue<5>(); // 5 }
The main advantage of this approach is that all calculations are performed at compile time, which enables its use in metaprogramming.
main
#include <iostream> // Template function to calculate the square of a non-type parameter I template<int I> int Square() { return I * I; } // Return the square of I int main() { // The result of Square<5>() is computed at compile time int b = Square<5>(); // b will be initialized to 25 }
When you run the compiled program, the value of b
will already be 25
. The calculation is performed at compile time, resulting in zero runtime operations.
Swipe to show code editor
Complete the template function and find the sum of options and operations variables.
- Complete the template function.
- Change the type of the
options
andoperations
variables to make it possible to pass them as a template parameters.
¡Gracias por tus comentarios!
When defining a template, we can not only specify types but also pass values to those types. This means we can create templates that behave differently based on the values provided but we will return to it a little bit later.
main
#include <iostream> // Non-type parameter means we won't use typename // Instead you can use an actual type for it template<int N> void PrintValue() { std::cout << N << std::endl; } int main() { // Call the template function with a literal integer PrintValue<5>(); // 5 }
The main advantage of this approach is that all calculations are performed at compile time, which enables its use in metaprogramming.
main
#include <iostream> // Template function to calculate the square of a non-type parameter I template<int I> int Square() { return I * I; } // Return the square of I int main() { // The result of Square<5>() is computed at compile time int b = Square<5>(); // b will be initialized to 25 }
When you run the compiled program, the value of b
will already be 25
. The calculation is performed at compile time, resulting in zero runtime operations.
Swipe to show code editor
Complete the template function and find the sum of options and operations variables.
- Complete the template function.
- Change the type of the
options
andoperations
variables to make it possible to pass them as a template parameters.