template <class T>
struct remove_reference
{
typedef see-below type;
};
type: 类型和T一样,但是移除了引用修饰符
头文件: #include <boost/type_traits/remove_reference.hpp> 或者#include <boost/type_traits.hpp>
Expression | Result Type |
remove_reference<int>::type | int |
remove_reference<int const&>::type | int const |
remove_reference<int&&>::type | int |
remove_reference<int*>::type | int* |
remove_reference<int*&>::type | int* |
源码分析:
// remove_rvalue_ref模板类的功能是移除一个类型的左值引用
template <class T>
struct remove_rvalue_ref
{
typedef T type;
};
template <class T>
struct remove_rvalue_ref<T&&>
{
typedef T type;
};
// remove_reference类模板的功能是移除一个类型的左值引用类型,引用类型,
// 对于非引用类型或左值引用类型
template < typename T >
struct remove_reference
{
typedef typename remove_rvalue_ref<T>::type type;
};
// 对于引用类型的特化
template < typename T >
struct remove_reference<T&>
{
typedef T type;
};