Namespaces
Variants

std:: is_swappable_with, std:: is_swappable, std:: is_nothrow_swappable_with, std:: is_nothrow_swappable

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
is_swappable_with is_swappable is_nothrow_swappable_with is_nothrow_swappable
(C++17) (C++17) (C++17) (C++17)

Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T, class U >
struct is_swappable_with ;
(1) (C++17 起)
template < class T >
struct is_swappable ;
(2) (C++17 起)
template < class T, class U >
struct is_nothrow_swappable_with ;
(3) (C++17 起)
template < class T >
struct is_nothrow_swappable ;
(4) (C++17 起)
1) 若表达式 swap ( std:: declval < T > ( ) , std:: declval < U > ( ) ) swap ( std:: declval < U > ( ) , std:: declval < T > ( ) ) using std:: swap ; 后(参见 可交换 )均能在不求值上下文中合法形成,则提供等于 true 的成员常量 value 。否则 value false
访问检查 的执行方式如同在与两种类型都无关的上下文中进行。
3) (1) 相同,但已知 (1) 中两个表达式的求值均不会抛出异常。
类型特性 成员常量 value 的值
T 是可引用类型 T 不是可引用类型
(2) std :: is_swappable_with < T & , T & > :: value false
(4) std :: is_nothrow_swappable_with < T & , T & > :: value

如果 T U 不是完整类型、(可能带有 cv 限定符的) void ,或未知边界数组,则行为未定义。

如果上述模板的实例化直接或间接依赖于不完整类型,且该实例化在该类型被假设完成时可能产生不同结果,则行为是未定义的。

如果程序对本页面描述的任何模板添加特化,则行为未定义。

目录

辅助变量模板

template < class T, class U >
inline constexpr bool is_swappable_with_v = is_swappable_with < T, U > :: value ;
(自 C++17 起)
template < class T >
inline constexpr bool is_swappable_v = is_swappable < T > :: value ;
(自 C++17 起)
template < class T, class U >

inline constexpr bool is_nothrow_swappable_with_v =

is_nothrow_swappable_with < T, U > :: value ;
(自 C++17 起)
template < class T >

inline constexpr bool is_nothrow_swappable_v =

is_nothrow_swappable < T > :: value ;
(自 C++17 起)

继承自 std:: integral_constant

成员常量

value
[static]
T 可与 U 交换则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool 类型,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

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

注释

此特性不会检查交换表达式直接上下文之外的任何内容:如果对 T U 的使用会触发模板特化、隐式定义的特殊成员函数生成等操作,且这些操作存在错误,那么即使 std :: is_swappable_with < T, U > :: value 能够编译并通过计算得到 true ,实际的交换操作可能仍无法编译通过。

示例

参见

交换两个对象的值
(函数模板)
检查类型是否具有移动赋值运算符
(类模板)
指定类型可交换或两个类型可相互交换
(概念)