Template Type Deduction

Template Type Deduction in C++Photo by Fotis Fotopoulos on UnsplashWhen we talk about C++, auto should be a compelling feature. Nevertheless, to understand the type deduction of auto, we should first take a look at the template type deduction. The cont…


This content originally appeared on Level Up Coding - Medium and was authored by Pin Loon Lee

Template Type Deduction in C++

Photo by Fotis Fotopoulos on Unsplash

When we talk about C++, auto should be a compelling feature. Nevertheless, to understand the type deduction of auto, we should first take a look at the template type deduction. The content for this post is borrowed from "Item 1: Understand template type deduction" in Effective Modern C++.

Template type deduction happens when there is not explicitly-specified template arguments. One of the simple examples to show explicitly-specified template arguments is as below.

template<typename T>
void f(const T& param)
int x = 0; 
f<double>(x);

To explain more about template type deduction, there are two keywords that we will be using which are T and ParameterType as shown below

template<typename T>
void f(ParameterType param)
  • T is the type of the template argument while ParameterType is the type of function argument param
  • T and ParameterType could be but not necessarily be the same.

For calling of function f(expr) below

template<typename T>
void f(const T& param)
int x = 0;
f(x);

T is deduced as int while ParameterType is deduced as const int &.

To deduce type for T , it is dependent not only on type of expr, but also on the form of ParameterType.

There are three cases where

  • ParameterType is a pointer or reference type, but not a universal reference
  • ParameterType is a universal reference
  • ParameterType is neither a pointer nor a reference

ParameterType is a Pointer or Reference type, but not a universal reference

template<typename T>
void f1(T&) {
std::cout << "T is ";
if (std::is_const_v<T>) {
std::cout << "const ";
}
std::cout << typeid(T).name() << std::endl;
}

If now we run the code of

int main() {
int x = 27;
const int cx = x;
const int& rx = x;
f1(x);
f1(cx);
f1(rx);
}

The output would be

T is i
T is const i
T is const i

rx’s reference-ness is ignored during type deduction and this works the same for pointer.

If we explicitly write the const for the template function argument, let's check what would T be deduced as

template <typename T>
void f2(const T&) {
std::cout << "T is ";
if (std::is_const_v<T>) {
std::cout << "const ";
}
std::cout << typeid(T).name() << std::endl;
}

If we run the code again by using f2, the output would be

T is i
T is i
T is i

For the three cases, T is deduced as int, in other words, the function that is being called is actually void f2(const int &)

ParameterType is a universal reference

  • For universal reference, we can use perfect forwarding to check which type of function is called. Please refer to my previous post for Perfect Forwarding
  • Now, let us use perfect forwarding, together with some function overloading to check which type of function is called.
void f(int&) { std::cout << "calling f(int &)" << std::endl; }
void f(const int&) { 
std::cout << "calling f(const int &)" << std::endl;
}
void f(int&&) { std::cout << "calling f(int &&)" << std::endl; }
template <typename T>
void f3(T&& param) {
f(std::forward<T>(param));
}

If we execute the code of

int main() {
int x = 27;
const int cx = x;
const int& rx = x;
f3(x);
f3(cx);
f3(rx);
f3(27);
}

We will get the output of

calling f(int &)
calling f(const int &)
calling f(const int &)
calling f(int &&)

The lvalueness and rvalueness is remained.

ParameterType is neither a pointer nor a reference

template <typename T>
void f4(T) {
std::cout << "T is ";
if (std::is_const_v<T>) {
std::cout << "const ";
}
std::cout << typeid(T).name() << std::endl;
}

When it is passed by value, const, volatile and reference would be ignored.

If we run the code again by using f4, the output would be

T is i
T is i
T is i

Thanks for reading! Hope you can understand template type deduction better now.


Template Type Deduction was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Pin Loon Lee


Print Share Comment Cite Upload Translate Updates
APA

Pin Loon Lee | Sciencx (2022-11-01T03:03:34+00:00) Template Type Deduction. Retrieved from https://www.scien.cx/2022/11/01/template-type-deduction/

MLA
" » Template Type Deduction." Pin Loon Lee | Sciencx - Tuesday November 1, 2022, https://www.scien.cx/2022/11/01/template-type-deduction/
HARVARD
Pin Loon Lee | Sciencx Tuesday November 1, 2022 » Template Type Deduction., viewed ,<https://www.scien.cx/2022/11/01/template-type-deduction/>
VANCOUVER
Pin Loon Lee | Sciencx - » Template Type Deduction. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/01/template-type-deduction/
CHICAGO
" » Template Type Deduction." Pin Loon Lee | Sciencx - Accessed . https://www.scien.cx/2022/11/01/template-type-deduction/
IEEE
" » Template Type Deduction." Pin Loon Lee | Sciencx [Online]. Available: https://www.scien.cx/2022/11/01/template-type-deduction/. [Accessed: ]
rf:citation
» Template Type Deduction | Pin Loon Lee | Sciencx | https://www.scien.cx/2022/11/01/template-type-deduction/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.