Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Passing Multiple Arguments with Different Types | Templates Usage
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

Passing Multiple Arguments with Different Types

In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.

cpp

main

copy
123456789101112131415
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

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

Everything was clear?

Section 2. Chapter 2
toggle bottom row

Passing Multiple Arguments with Different Types

In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.

cpp

main

copy
123456789101112131415
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

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

Everything was clear?

Section 2. Chapter 2
toggle bottom row

Passing Multiple Arguments with Different Types

In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.

cpp

main

copy
123456789101112131415
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

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

Everything was clear?

In previous section we create a function that prints a sum of two parameters. Because that was a template function we could pass any data type we want and everything worked.

cpp

main

copy
123456789101112131415
#include <iostream> // Though we can set the same template parameter to two function arguments // There won't be a way to specify different data types afterward template<typename T> void MultipleArguments(T a, T b) { std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; } // If you run this code you will get an error: // error: wrong number of template arguments (2, should be 1) int main() { MultipleArguments<char, bool>('a', true); }

Task

Each argument of a template function has to be a different data type.

  • Create enough template parameters for each argument of the function to set a data type

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