std::remove_reference

定义于头文件 <type_traits>
template< class T >
struct remove_reference;
(C++11 起)

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

目录

成员类型

名称 定义
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

参阅

(C++11)
检查类型是否为左值引用右值引用
(类模板)
添加左值右值引用到指定类型
(类模板)
(C++20)
std::remove_cvstd::remove_reference 结合
(类模板)

版本历史

  • (当前 | 先前 2017年11月18日 (六) 21:55Fruderica讨论 | 贡献 . . (1,682字节) (0). . (辅助) (撤销)
  • 当前 | 先前 2017年11月13日 (一) 04:44Fruderica讨论 | 贡献 . . (1,682字节) (-17). . (+ remove_cvref) (撤销)
  • 当前 | 先前 2017年7月22日 (六) 23:47Fruderica讨论 | 贡献 . . (1,699字节) (+10). . (撤销)
  • 当前 | 先前 2017年7月22日 (六) 23:47Fruderica讨论 | 贡献. . (1,689字节) (+233). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:19P12bot讨论 | 贡献 . . (1,456字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:54P12bot讨论 | 贡献 . . (1,456字节) (-109). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:05P12bot讨论 | 贡献 . . (1,565字节) (+273). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 12:44P12讨论 | 贡献 . . (1,292字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,292字节) (-40). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 04:09P12讨论 | 贡献 . . (1,332字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,332字节) (+1,332). . (Translated from the English version using Google Translate)