std::reference_constructs_from_temporary
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T, class U > struct reference_constructs_from_temporary; |
(C++23 起) | |
令 V
為 std::remove_cv_t<U>(如果 U
是標量型別或 cv void
),否則為 U
。如果 T
是引用型別,並且給定一個假設表示式 e,使得 decltype(e) 為 V
,變數定義 T ref(e); 是良構的,並且 將一個臨時物件繫結到 ref
,則提供等於 true 的成員常量 value
。否則,value
為 false。
如果 T
是到 const 但非 volatile 限定物件型別的左值引用型別或右值引用型別,則 std::remove_reference_t<T> 和 std::remove_reference_t<U> 都應是完整型別,cv void,或未知邊界陣列;否則行為未定義。
如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。
如果程式為 std::reference_constructs_from_temporary
或 std::reference_constructs_from_temporary_v
新增特化,則行為未定義。
目錄 |
[編輯] 輔助變數模板
template< class T, class U > inline constexpr bool reference_constructs_from_temporary_v = |
(C++23 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
true 如果 T 是引用型別,U 值可以在直接初始化中繫結到 T ,並且一個臨時物件將被繫結到引用;否則為 false。(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
std::integral_constant<bool, value> |
[編輯] 註解
std::reference_constructs_from_temporary
可用於拒絕某些總是產生懸空引用的情況。
如果編譯器已實現 CWG1696,也可以使用成員初始化列表來拒絕將臨時物件繫結到引用。
[編輯] 示例
執行此程式碼
#include <type_traits> static_assert(std::reference_constructs_from_temporary_v<int&&, int> == true); static_assert(std::reference_constructs_from_temporary_v<const int&, int> == true); static_assert(std::reference_constructs_from_temporary_v<int&&, int&&> == false); static_assert(std::reference_constructs_from_temporary_v<const int&, int&&> == false); static_assert(std::reference_constructs_from_temporary_v<int&&, long&&> == true); static_assert(std::reference_constructs_from_temporary_v<int&&, long> == true); int main() {}
[編輯] 參閱
(C++11)(C++11)(C++11) |
檢查型別是否具有針對特定引數的建構函式 (類模板) |
構造一個新的 tuple ( std::tuple<Types...> 的公開成員函式) | |
構造新的 pair ( std::pair<T1,T2> 的公開成員函式) | |
(C++17) |
用元組引數構造物件 (函式模板) |