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

Course Content

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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt