std::reference_constructs_from_temporary
来自 cppreference.cn
定义于头文件 <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) |
用元组参数构造对象 (函数模板) |