命名空间
变体
操作

std::remove_reference

来自 cppreference.cn
< cpp‎ | 类型
 
 
元编程库
类型特性
类型类别
(C++11)
(C++11)(DR*)
(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++26 中已弃用)
(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++23 中已弃用)
(C++11)(C++23 中已弃用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
编译时有理数算术
编译时整数序列
 
定义于头文件 <type_traits>
template< class T >
struct remove_reference;
(C++11 起)

如果类型 T 是引用类型,则提供成员 typedef type,它是 T 所引用的类型。否则 typeT

如果程序为 std::remove_reference 添加特化,则行为是未定义的。

目录

[编辑] 成员类型

名称 定义
类型 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>
#include <type_traits>
 
int main()
{
    std::cout << std::boolalpha;
 
    std::cout << "std::remove_reference<int>::type is int? "
              << std::is_same<int, std::remove_reference<int>::type>::value << '\n';
    std::cout << "std::remove_reference<int&>::type is int? "
              << std::is_same<int, std::remove_reference<int&>::type>::value << '\n';
    std::cout << "std::remove_reference<int&&>::type is int? "
              << std::is_same<int, std::remove_reference<int&&>::type>::value << '\n';
    std::cout << "std::remove_reference<const int&>::type is const int? "
              << std::is_same<const int,
                              std::remove_reference<const int&>::type>::value << '\n';
}

输出

std::remove_reference<int>::type is int? true
std::remove_reference<int&>::type is int? true
std::remove_reference<int&&>::type is int? true
std::remove_reference<const int&>::type is const int? true

[编辑] 另请参阅

检查类型是否为*左值引用*或*右值引用*
(类模板) [编辑]
向给定类型添加左值或右值引用
(类模板) [编辑]
结合了 std::remove_cvstd::remove_reference
(类模板) [编辑]