std::is_swa...pable_with,(3)
π std::is_swappable_with, std::is_swappable, std::is_nothrow_swappable_with
okay bestie, buckle up π this is a C++ Standard Library page, straight out of the <type_traits> header, and it's all about asking the universe (well, the compiler) the very important question: can these two things swap or not? β¨
β¨ SYNOPSIS β¨
so, defined in header <type_traits>, we got four templates, all of them <cyan>since C++17</cyan>:
template< class T, class U > (1) (since C++17)
struct is_swappable_with;
template< class T > (2) (since C++17)
struct is_swappable;
template< class T, class U > (3) (since C++17)
struct is_nothrow_swappable_with;
template< class T > (4) (since C++17)
struct is_nothrow_swappable;
(1)β if the expressionsswap(std::declval<T>(), std::declval<U>())andswap(std::declval<U>(), std::declval<T>())are both well-formed in unevaluated context afterusing std::swap;(see Swappable), this one gives you the member constantvalueequal totrue. otherwise?valueisfalse. π and heads up: access checks are performed as if from a context unrelated to either type.(3)β same as(1), but the evaluations of both expressions from(1)are known not to throw exceptions. no cap. π
π¦ the value of the member constant value
okay this is the chart, and it's giving very "iykyk" so listen up:
(2)β whenTis a referenceable type:std::is_swappable_with<T&, T&>::value- when
Tis not a referenceable type:false
- when
(4)β whenTis a referenceable type:std::is_nothrow_swappable_with<T&, T&>::value- when
Tis not a referenceable type:false
- when
π undefined behavior zone (read this fr fr)
ifTorUis not a complete type, is (possibly cv-qualified)void, or is an array of unknown bound, the behavior is undefined. not cute, not slay, just undefined. π
if an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
if the program adds specializations for any of the templates described on this page, the behavior is undefined. periodt. π«Ά
π MEMBER CONSTANTS π
valueβtrueifTis swappable withU,falseotherwise. it's a[static]public static member constant β¨
πΈ MEMBER FUNCTIONS πΈ
operator boolβ converts the object tobool, returnsvalue(public member function π)operator()β returnsvalue(C++14) (public member function π)
π MEMBER TYPES π
value_typeβ defined asbooltypeβ defined asstd::integral_constant<bool, value>
π§ NOTES π§
lowkey important: this trait does not check anything outside the immediate context of the swap expressions. like, if the use of T or U would trigger template specializations, generation of implicitly-defined special member functions etc., and those have errors β the actual swap may not compile even if std::is_swappable_with<T, U>::value compiles and evaluates to true. the way that's a trap ππ
π EXAMPLE π
This section is incomplete Reason: no example
(yeah bestie, no example. we're all just vibing on trust here.)
π SEE ALSO π
swapβ swaps the values of two objects (function template)is_move_assignable,is_trivially_move_assignable,is_nothrow_move_assignableβ checks if a type has a move assignment operator (C++11) (class template) (C++11) (C++11)swappable,swappable_withβ specifies that a type can be swapped or that two types can be swapped with each other (C++20) (concept)
β category
- Todo no example
and that's the whole page, bestie! π«Ά this iconic little number is served up fresh from http://cppreference.com, dated 2024.06.10 β go forth and swap responsibly πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms