remove_reference源码
实现方式
这里的实现非常简单,就是去掉了类型的左值引用和右值引用,直接返回类型的基本内容。
性能开销
是否只在编译期间出现不会有任何运行时开销:是的。
/// remove_reference
template<typename _Tp>
struct remove_reference
{ typedef _Tp type; };
template<typename _Tp>
struct remove_reference<_Tp&>
{ typedef _Tp type; };
template<typename _Tp>
struct remove_reference < _Tp&& >
{ typedef _Tp type; };
代码示例:
原地址:http://www.cplusplus.com/reference/type_traits/remove_reference/?kw=remove_reference
// remove_reference
#include <iostream>
#include <type_traits>
int main() {
typedef int&& rval_int;
typedef std::remove_reference<int>::type A;
typedef std::remove_reference<int&>::type B;
typedef std::remove_reference<int&&>::type C;
typedef std::remove_reference<rval_int>::type D;
std::cout << std::boolalpha;
std::cout << "typedefs of int:" << std::endl;
std::cout << "A: " << std::is_same<int,A>::value << std::endl;
std::cout << "B: " << std::is_same<int,B>::value << std::endl;
std::cout << "C: " << std::is_same<int,C>::value << std::endl;
std::cout << "D: " << std::is_same<int,D>::value << std::endl;
return 0;
}
应用场景
需要消除类型中的引用操作的地方。
目前理解主要用于模版类型参数为别名的情况,消除别名中的引用操作内容,还原出基本类型。
参考:gcc8源码