std::reference_constructs_from_temporary
来自cppreference.com
在标头 <type_traits> 定义 | ||
template< class T, class U > struct reference_constructs_from_temporary; | (C++23 起) | |
若 U
为标量类型或 cv void
,则令 V
为 std::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_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 [静态] | 如果 T 为引用类型,能在直接初始化中绑定 U 到 T ,且临时对象会被绑定到引用那么是 true,否则是 false (公开静态成员常量) |
成员函数
operator bool | 将对象转换到 bool,返回 value (公开成员函数) |
operator() (C++14) | 返回 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() {}
参阅
(C++11)(C++11)(C++11) | 检查类型是否带有针对特定实参的构造函数 (类模板) |
构造新的 tuple ( std::tuple<Types...> 的公开成员函数) | |
构造新的 pair ( std::pair<T1,T2> 的公开成员函数) | |
(C++17) | 以一个实参元组构造对象 (函数模板) |