Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Using Template Function | Creating First Template
C++ Templates
course content

Course Content

C++ Templates

C++ Templates

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

Using Template Function

Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.

cpp

main

copy
123456789101112
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }

As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template, comes into play.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }

Note

It doesn't matter what type you specify for this example, so you can change void to any type you want. However, specifying the type is mandatory.

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 3
toggle bottom row

Using Template Function

Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.

cpp

main

copy
123456789101112
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }

As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template, comes into play.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }

Note

It doesn't matter what type you specify for this example, so you can change void to any type you want. However, specifying the type is mandatory.

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 1. Chapter 3
toggle bottom row

Using Template Function

Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.

cpp

main

copy
123456789101112
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }

As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template, comes into play.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }

Note

It doesn't matter what type you specify for this example, so you can change void to any type you want. However, specifying the type is mandatory.

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Congratulations on creating your first template! Now, let's make it do something, like printing a message. Go ahead and call it inside the main function to see it in action.

cpp

main

copy
123456789101112
#include <iostream> template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { MyFirstTemplate(); }

As you can see, an error arises when we try to call it as if it were a simple function. This is because it's no longer just a simple function. This is where the template parameter, specified inside the brackets after the keyword template, comes into play.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> // The keyword for template parameter called typename // Essentially we are just creating an alias for a type we will use // In our case the name of the typename is Name // VVVV template<typename Name> void MyFirstTemplate() { std::cout << "c<>definity" << std::endl; } int main() { // In order to call the template function properly // We need to specify any type inside the brackets // VVVV MyFirstTemplate<void>(); // ^^^^ // This tells the template to use void as the type for Name }

Note

It doesn't matter what type you specify for this example, so you can change void to any type you want. However, specifying the type is mandatory.

Task

  • Specify the name for typename
  • Pass the typename as a parameter for typeid()
  • Call the function template three times specifying the next data types in the < > brackets:
    • <int> data type
    • <float> data type
    • <double> data type

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 3
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt