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 否则(公共静态成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公共成员函数) |
operator() (C++14) |
返回 value (公共成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
type
|
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) |
使用参数 tuple 构造对象 (函数模板) |