CppDS.com

C++ 98 11 14 17 20 手册

std::remove_reference

来自cppreference.com
< cpp‎ | types
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等字符串转换
(C++17)
(C++17)
 
类型支持
基本类型
基础类型
定宽整数类型 (C++11)
数值极限
C 数值极限接口
运行时类型信息
类型特性
类型类别
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20 前)
(C++11)(C++20 中弃用)
(C++11)
类型特性常量
元函数
(C++17)
常量求值语境
受支持操作
关系与属性查询
类型修改
(C++11)(C++11)(C++11)
remove_reference
(C++11)
类型变换
(C++11)
(C++11)
(C++17)
(C++11)(C++20 前)(C++17)
 
定义于头文件 <type_traits>
template< class T >
struct remove_reference;
(C++11 起)

若类型 T 为引用类型,则提供成员 typedef type ,其为 T 所引用的类型。否则 typeT

添加 remove_reference 的特化的程序行为未定义。

成员类型

 
名称 定义
type T 所引用的类型,或若 T 不是引用则为 T

辅助类型

template< class T >
using remove_reference_t = typename remove_reference<T>::type;
(C++14 起)

可能的实现

template< class T > struct remove_reference      {typedef T type;};
template< class T > struct remove_reference<T&>  {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};

示例

#include <iostream> // std::cout
#include <type_traits> // std::is_same
 
template<class T1, class T2>
void print_is_same() {
  std::cout << std::is_same<T1, T2>() << '\n';
}
 
int main() {
  std::cout << std::boolalpha;
 
  print_is_same<int, int>();
  print_is_same<int, int &>();
  print_is_same<int, int &&>();
 
  print_is_same<int, std::remove_reference<int>::type>();
  print_is_same<int, std::remove_reference<int &>::type>();
  print_is_same<int, std::remove_reference<int &&>::type>();
}

输出:

true
false
false
true
true
true

参阅

检查类型是否为左值引用右值引用
(类模板)
向给定类型添加左值右值引用
(类模板)
std::remove_cvstd::remove_reference 结合
(类模板)
关闭