请问模板结构的含义
请教模板结构的<T&&>含义
例如下三种模板结构体的定义,本人只见过第一种,很好理解和模板类一样。但是第二种和第三种就崩溃了,啥意思?为何要这样做?请知道的说说是什么意思?先谢!
// TEMPLATE _Remove_reference
template<class _Ty>
struct _Remove_reference
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&>
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&&>
{ // remove rvalue reference
typedef _Ty _Type;
};
------解决方案--------------------
特化 ,分别匹配 引用类型 和右值引用
例如下三种模板结构体的定义,本人只见过第一种,很好理解和模板类一样。但是第二种和第三种就崩溃了,啥意思?为何要这样做?请知道的说说是什么意思?先谢!
// TEMPLATE _Remove_reference
template<class _Ty>
struct _Remove_reference
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&>
{ // remove reference
typedef _Ty _Type;
};
template<class _Ty>
struct _Remove_reference<_Ty&&>
{ // remove rvalue reference
typedef _Ty _Type;
};
------解决方案--------------------
特化 ,分别匹配 引用类型 和右值引用
- C/C++ code
#include<stdio.h> #include<typeinfo.h> // TEMPLATE _Remove_reference template<class _Ty> struct _Remove_reference { // remove reference typedef _Ty _Type; }; template<class _Ty> struct _Remove_reference<_Ty&> { // remove reference typedef _Ty _Type; }; template<class _Ty> struct _Remove_reference<_Ty&&> { // remove rvalue reference typedef _Ty _Type; }; int main() { puts( typeid(_Remove_reference<int>::_Type).name() );//匹配第一个 puts( typeid(_Remove_reference<int&>::_Type).name() );//匹配第二个 puts( typeid(_Remove_reference<int&&>::_Type).name() );//匹配第三个 }
------解决方案--------------------
------解决方案--------------------