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

Kursinnhold

C++ Templates

C++ Templates

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

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

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.

Oppgave

Swipe to start coding

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.

Løsning

cpp

solution

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
toggle bottom row

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

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.

Oppgave

Swipe to start coding

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.

Løsning

cpp

solution

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt