当前位置:网站首页>Removal and addition of reference types in template and generic programming
Removal and addition of reference types in template and generic programming
2022-07-27 01:57:00 【Hair like snow ty】
One 、 Removal of reference types
c++11 The standard library provides a std::remove_reference Class template , If the template parameter passed in is a reference type , Will delete the reference part of this reference type in China , Let's see how it works first
template<typename T1,typename T2>
void print_is_same()
{
cout << "T1 The type of :" << typeid(T1).name() << endl;
cout << "T2 The type of :" << typeid(T2).name() << endl;
cout << "T1 and T2 Whether it is equal or not :" << std::is_same<T1, T2>::value << endl;
}
int main()
{
std::remove_reference_t<int> a;// and std::remove_reference<int>::type identical
std::remove_reference<int&>::type b;
std::remove_reference<int&&>::type c;
print_is_same<decltype(a),decltype(b)>();
print_is_same<decltype(a), decltype(c)>();
system("pause");
return 0;
}
result :
here std::remove_reference It's equivalent to a trait Class template , How can I realize a similar function ?
The complete code is as follows :
template<typename T>
struct RemoveReference
{
using Type = T;
};
template<typename T>
struct RemoveReference<T &>
{
using Type = T;
};
template<typename T>
struct RemoveReference<T&&>
{
using Type = T;
};
template<typename T>
using RemoveReference_t = typename RemoveReference<T>::Type;
int main()
{
RemoveReference_t<int> a;
RemoveReference_t<int &> b;
RemoveReference_t<int&&> c;
print_is_same<decltype(a),decltype(b)>();
print_is_same<decltype(a), decltype(c)>();
system("pause");
return 0;
}
result :
Two 、 Addition of reference types
The so-called increase of reference types , In fact, it is to create an lvalue or lvalue reference according to a given type .
c++11 The standard provides a std::add_lvalue_reference Class template , Used to pass in a type , Return the corresponding lvalue reference type of this type . For example, pass in a int, return int & type . Again c++11 There is also a std::add_rvalue_reference Class template , Used to pass in a type , Return the right value reference type corresponding to this type , For example, pass in a int, Return to one int &&.
Accordingly , also std::is_lvalue_reference and std::is_rvalue_reference Class template , It is used to judge whether a type is an lvalue reference type or an lvalue application type .
Look at the basic use of these class templates :
int main()
{
int a = 15;
std::add_lvalue_reference<decltype(a)>::type b = a;
std::add_rvalue_reference_t<decltype(a)> c = 16;
using btype = add_lvalue_reference_t<int>;
cout << std::is_same<int &, btype>() << endl;//1
using ctype = add_rvalue_reference_t<int>;
cout << std::is_same<int&&, ctype>() << endl;//1
cout << std::is_lvalue_reference<btype>() << endl;//1
cout << std::is_rvalue_reference<ctype>() << endl;//1
system("pause");
return 0;
}
result :
How can I realize such an extractor ?
template<typename T>
struct AddLvalueReference
{
using type = T &;
};
template<typename T>
using AddLvalueReference_t = typename AddLvalueReference<T>::type;
void main()
{
int anew = 15;
AddLvalueReference_t<decltype(anew)> bnew = anew;
int &&anew2 = 16;
AddLvalueReference_t<decltype(anew2)> bnew2 = anew2;
cout << is_same<int&, decltype(bnew)>() << endl;
cout << is_same<int&, decltype(bnew2)>() << endl;
system("pause");
}
The above code ,bnew The type of int &, and bnew2 The type of is not so obvious , Let's analyze ,
(1)anew2 yes int && Right value reference type .
(2) therefore decltype(anew2) It must be int && type , that ,AddLvalueReference_t<int &&> What's the return ?
(3)AddLvalueReference Class template implementation code using type = T&, There will be int && Brought in is using type = T && &; Collapse rules by reference , There is an lvalue reference , The result must be an lvalue reference , therefore type = int &;
(4) Final bnew2 The type of int &.
result :
similar , You can also write a reference type that adds a right value trait, as follows :
template<typename T>
struct AddRvalueReference
{
using type = T &&;
};
template<typename T>
using AddRvalueReference_t = typename AddRvalueReference<T>::type;
边栏推荐
- 24ssh service
- C language foundation Gobang, very easy to understand, detailed explanation, easy to use
- LAMP.
- Shell (7) case statement
- Homework 1-4 learning notes
- 27shell之条件语句
- Source code compilation and installation lamp
- [polymorphism] the detailed introduction of polymorphism is simple and easy to understand
- Problems and solutions of paddleocr packaging
- Mysql数据库-面试题
猜你喜欢

Shell (10) array and bubble sort

System safety and Application

Machine learning exercise 7 - K-means and PCA (principal component analysis)

25pxe efficient batch network installation

SSH and NFS services

Small project - self connected campus network

MySQL单表查询练习

24SSH服务

【CANN训练营】走进媒体数据处理(下)

Suggestions for beginners of MySQL (strongly recommended ~ don't regret ~ ~)
随机推荐
系统动力学专拓考试重点总结
29shell function
27shell conditional statement
信息获取技术复习
PHP exit codes description
String容器的底层实现
Shell编程规范与变量
FTP service
数字图像处理重点总结复习
虚拟化技术KVM
Virtualization technology KVM
HarmonyOS图像处理应用开发实战直播笔记
D - Difference HDU - 5936
shell课程总结
23nfs shared storage service
DNS
mysql的安装
Use ECs and OSS to set up personal network disk
introduction
The bottom implementation of vector container