当前位置:网站首页>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;
边栏推荐
猜你喜欢
随机推荐
(atcoder contest 144) f - fork in the road (probability DP)
Desktop solution summary
Constructor, copy function and destructor
Identify artifact MX yolov3
24ssh service
mysql的安装
GDB的使用
MySQL索引
29shell函数
31正则表达式
PXE experiment
Virtualization technology KVM
21dns domain name resolution
Source code compilation and installation lamp
DevEco-Could not resolve com.huawei.ohos:hap:2.4.5.0.错误
Linux部署MYSQL
【CANN训练营】走进媒体数据处理(下)
Shell (10) array and bubble sort
Shell script - backup, update and rollback of files
Small project - self connected campus network



![[cann training camp] enter media data processing 1](/img/6c/76d3784203af18a7dee199c3a7fd24.png)





