Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Non-type Template Parameters | Templates Usage
C++ Templates
course content

Conteúdo do Curso

C++ Templates

C++ Templates

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

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

cpp

main

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

cpp

main

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

Tarefa
test

Swipe to show code editor

Complete the template function and find the sum of options and operations variables.

  1. Complete the template function.
  2. Change the type of the options and operations variables to make it possible to pass them as a template parameters.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
toggle bottom row

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

cpp

main

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

cpp

main

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

Tarefa
test

Swipe to show code editor

Complete the template function and find the sum of options and operations variables.

  1. Complete the template function.
  2. Change the type of the options and operations variables to make it possible to pass them as a template parameters.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
toggle bottom row

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

cpp

main

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

cpp

main

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

Tarefa
test

Swipe to show code editor

Complete the template function and find the sum of options and operations variables.

  1. Complete the template function.
  2. Change the type of the options and operations variables to make it possible to pass them as a template parameters.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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.

cpp

main

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

cpp

main

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

Tarefa
test

Swipe to show code editor

Complete the template function and find the sum of options and operations variables.

  1. Complete the template function.
  2. Change the type of the options and operations variables to make it possible to pass them as a template parameters.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt