std:: remove_reference
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct remove_reference ; |
(C++11 起) | |
如果类型
T
是引用类型,则提供成员类型定义
type
,该类型为
T
所引用的类型。否则
type
为
T
。
如果程序为
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 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
参见
|
(C++11)
|
检查类型是否为
左值引用
或
右值引用
(类模板) |
|
(C++11)
(C++11)
|
为给定类型添加
左值
或
右值
引用
(类模板) |
|
(C++20)
|
组合
std::remove_cv
与
std::remove_reference
(类模板) |