std::reference_constructs_from_temporary
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    | 在标头  <type_traits>定义 | ||
| template< class T, class U > struct reference_constructs_from_temporary; | (C++23 起) | |
令 V 若 U 为标量类型或 cv void 则为 std::remove_cv_t<U> ,否则为 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 或未知边界数组;否则行为未定义。
若模板的实例直接或间接依赖不完整类型,而假如将令类型完整则实例化能导致不同的结果,则行为未定义。
添加 reference_constructs_from_temporary 或 reference_constructs_from_temporary_v 的特化的程序行为未定义。
辅助变量模板
| template< class T, class U > inline constexpr 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> #include <iostream> int main() { std::cout << std::boolalpha << std::reference_constructs_from_temporary_v<int&&, int> << '\n' << std::reference_constructs_from_temporary_v<const int&, int> << '\n' << std::reference_constructs_from_temporary_v<int&&, int&&> << '\n' << std::reference_constructs_from_temporary_v<const int&, int&&> << '\n' << std::reference_constructs_from_temporary_v<int&&, long&&> << '\n'; << std::reference_constructs_from_temporary_v<int&&, long> << '\n'; }
输出:
true true false false true true
参阅
| (C++11)(C++11)(C++11) | 检查类型是否带有针对特定实参的构造函数 (类模板) | 
| (C++11) | 构造新的 tuple( std::tuple<Types...>的公开成员函数) | 
| 构造新的 pair( std::pair<T1,T2>的公开成员函数) | |
| (C++17) | 以一个实参元组构造对象 (函数模板) |