Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Class Template Argument Deduction | Class Templates
C++ Templates
course content

Зміст курсу

C++ Templates

C++ Templates

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

book
Class Template Argument Deduction

You might have already used some of them without realizing they were templates at first, possibly due to Class Template Argument Deduction (CTAD).

With C++17, we can take advantage of Class Template Argument Deduction (CTAD). This feature allows us to create instances of our template classes without explicitly specifying the template types. Instead, the compiler infers the types based on the constructor parameters. Let’s look at some examples using std::pair.

cpp

main

copy
12345678
#include <iostream> #include <utility> // for std::pair int main() { std::pair<int, char> my_pair(1, 'a'); std::cout << my_pair.first << " : " << my_pair.second << std::endl; }

Note

Previously, this was the only way to create an instance of std::pair. However, now it is possible to omit the <int, char> part. Try removing it and run the code again.

Class template argument deduction is only performed if no template argument list is present. If a template argument list is specified, deduction does not take place.

cpp

main

copy
12345678
#include <iostream> #include <utility> // for std::pair int main() { std::pair<> my_pair(1, 'a'); // Error std::cout << my_pair.first << " : " << my_pair.second << std::endl; }

When creating an object with a single argument that matches a specific type of a class template, CTAD prefers to use that type directly, simplifying instantiation for user-defined template classes. For instance, with a template class like Box, users can instantiate it without specifying type arguments when providing constructor arguments.

cpp

main

copy
123456789101112131415
#include <iostream> template <typename T> class Box { T value; public: Box(T value): value(value) {} }; int main() { // No need to write Box<int> a{1}; Box a{1}; // Deduction: Box<int> Box b{a}; // Deduction: Box<int>, not Box<Box<int>> }

CTAD can make maintaining code easier. If the underlying types change, there’s less code to update, leading to fewer opportunities for errors.

h

vector

copy
12345678
template <typename T> class Vector3D { T x, y, z; public: Vector3D(T x, T y, T z) : x(x), y(y), z(z) {} }; Vector3D vec{1.0, 2.0, 3.0}; // CTAD deduces Vector3D<double>

When using containers or wrappers with specific constructors, CTAD can simplify the code. Suppose you have a custom Matrix or Vector class that wraps an underlying data type. Using CTAD here can help ensure that type deduction happens automatically based on the arguments provided, improving readability and flexibility.

What is Class Template Argument Deduction (CTAD)?

What is Class Template Argument Deduction (CTAD)?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt