- Learning C++ Functional Programming
- Wisnu Anggoro
- 462字
- 2021-07-02 20:51:35
Querying the type of an expression using the decltype keyword
We discussed in the preceding section that the auto keyword can automatically deduce the type of the variable based on the type of values it stores. The keyword can also deduce the function's return type based on the type of its return value. Now, let's combine the auto keyword and the decltype keyword to gain the power of modern C++.
Before we combine the two keywords, we will find out what the decltype keyword is used for--it is used for asking the type of an object or an expression. Let's take a look at the following several lines of trivial variable declaration:
const int func1();
const int& func2();
int i;
struct X { double d; };
const X* x = new X();
Now, based on the preceding code, we can declare other variables using the decltype keyword as follows:
// Declaring const int variable
// using func1() type
decltype(func1()) f1;
// Declaring const int& variable
// using func2() type
decltype(func2()) f2;
// Declaring int variable
// using i type
decltype(i) i1;
// Declaring double variable
// using struct X type
decltype(x->d) d1; // type is double
decltype((x->d)) d2; // type is const double&
As we can see in the preceding code, we can specify the type of an object based on another object's type. Now, let's suppose we need to refactor the preceding add() method to become a template. Without the auto and decltype keyword, we will have the following template implementation:
template<typename I, typename J, typename K>
K add(I i, J j)
{
return i + j;
}
Fortunately, since the auto keyword can specify the return type of the function, which is a trailing return type, and the decltype keyword can deduce the type based on the expression, we can refactor the preceding template as follows:
template<typename I, typename J>
auto add(I i, J j) -> decltype(i + j)
{
return i + j;
}
To prove, let's compile and run the following decltype.cpp code. We will use the following template to calculate the addition of two different value types--integer and double:
/* decltype.cpp */
#include <iostream>
// Creating template
template<typename I, typename J>
auto add(I i, J j) -> decltype(i + j)
{
return i + j;
}
auto main() -> int
{
std::cout << "[decltype.cpp]" << std::endl;
// Consuming the template
auto d = add<int, double>(2, 2.5);
// Displaying the preceding variables' type
std::cout << "result of 2 + 2.5: " << d << std::endl;
return 0;
}
The compilation process should run smoothly without error. We will see the following output on the screen if we run the preceding code:

As we can see, we have successfully combined the auto and decltype keyword to create a template simpler than we usually do before the modern C++ is announced.
- Dynamics 365 for Finance and Operations Development Cookbook(Fourth Edition)
- 數(shù)據(jù)科學(xué)實(shí)戰(zhàn)手冊(cè)(R+Python)
- Testing with JUnit
- Interactive Data Visualization with Python
- MATLAB圖像處理超級(jí)學(xué)習(xí)手冊(cè)
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- JavaScript+Vue+React全程實(shí)例
- Java Web開(kāi)發(fā)技術(shù)教程
- Python全棧數(shù)據(jù)工程師養(yǎng)成攻略(視頻講解版)
- C#開(kāi)發(fā)案例精粹
- Mastering React
- RealSenseTM互動(dòng)開(kāi)發(fā)實(shí)戰(zhàn)
- Kivy Cookbook
- Visual Basic程序設(shè)計(jì)(第三版)
- MyBatis 3源碼深度解析