◐ Shell
clean mode source ↗

std::remove_reference - cppreference.com

来自cppreference.com

template< class T >
struct remove_reference;
(C++11 起)

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

如果程序添加了 std::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>
#include <type_traits>

int main()
{
    std::cout << std::boolalpha;

    std::cout << "std::remove_reference<int>::type 是 int? "
              << std::is_same<int, std::remove_reference<int>::type>::value << '\n';
    std::cout << "std::remove_reference<int&>::type 是 int? "
              << std::is_same<int, std::remove_reference<int&>::type>::value << '\n';
    std::cout << "std::remove_reference<int&&>::type 是 int? "
              << std::is_same<int, std::remove_reference<int&&>::type>::value << '\n';
    std::cout << "std::remove_reference<const int&>::type 是 const int? "
              << std::is_same<const int,
                              std::remove_reference<const int&>::type>::value << '\n';
}

输出:

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

参阅