名稱空間
變體
操作

std::reference_constructs_from_temporary

來自 cppreference.com
< 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)
型別轉換
(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, class U >
struct reference_constructs_from_temporary;
(C++23 起)

Vstd::remove_cv_t<U>(如果 U 是標量型別或 cv void),否則為 U。如果 T 是引用型別,並且給定一個假設表示式 e,使得 decltype(e)V,變數定義 T ref(e); 是良構的,並且 將一個臨時物件繫結到 ref,則提供等於 true 的成員常量 value。否則,valuefalse

如果 T 是到 const 但非 volatile 限定物件型別的左值引用型別或右值引用型別,則 std::remove_reference_t<T>std::remove_reference_t<U> 都應是完整型別cv void,或未知邊界陣列;否則行為未定義。

如果上述模板的例項化直接或間接依賴於不完整型別,並且該例項化在該型別假設完成時可能產生不同的結果,則行為未定義。

如果程式為 std::reference_constructs_from_temporarystd::reference_constructs_from_temporary_v 新增特化,則行為未定義。

目錄

[編輯] 輔助變數模板

template< class T, class U >

inline constexpr bool reference_constructs_from_temporary_v =

    std::reference_constructs_from_temporary<T, U>::value;
(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() {}

[編輯] 參閱

檢查型別是否具有針對特定引數的建構函式
(類模板) [編輯]
構造一個新的 tuple
(std::tuple<Types...> 的公開成員函式) [編輯]
構造新的 pair
(std::pair<T1,T2> 的公開成員函式) [編輯]
用元組引數構造物件
(函式模板) [編輯]