std::remove_reference
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct remove_reference; |
(C++11 起) | |
如果型別 T
是引用型別,則提供成員 typedef type
,它是 T
所引用的型別。否則 type
是 T
。
如果程式為 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
[編輯] 另請參閱
(C++11) |
檢查型別是否為*左值引用*或*右值引用* (類模板) |
(C++11)(C++11) |
向給定型別新增左值或右值引用 (類模板) |
(C++20) |
結合了 std::remove_cv 和 std::remove_reference (類模板) |