◐ Shell
clean mode source ↗

std::reference_constructs_from_temporary - cppreference.com

来自cppreference.com

template< class T, class U >
struct reference_constructs_from_temporary;
(C++23 起)

U 为标量类型或 cv void,则令 Vstd::remove_cv_t<U>,否则为 U。若 T 为引用类型,并且给定假想的表达式 e 使得 decltype(e)V,而变量定义 T ref(e); 良构并绑定临时对象ref,则提供的成员常量 value 等于 true。否则 value 等于 false

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

成员常量

如果 T 为引用类型,能在直接初始化中绑定 UT,且临时对象会被绑定到引用那么是 true,否则是 false
(公开静态成员常量)

成员函数

将对象转换到 bool,返回 value
(公开成员函数)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

注解

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() {}

参阅