std::is_swa...pable_with,(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 expressions swap(std::declval<T>(), std::declval<U>()) and swap(std::declval<U>(), std::declval<T>()) are both well-formed in unevaluated context after using std::swap; (see Swappable), this one gives you the member constant value equal to true. otherwise? value is false. πŸ’€ 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) β€” when T is a referenceable type: std::is_swappable_with<T&, T&>::value
    • when T is not a referenceable type: false
  • (4) β€” when T is a referenceable type: std::is_nothrow_swappable_with<T&, T&>::value
    • when T is not a referenceable type: false

πŸ’€ undefined behavior zone (read this fr fr)

if T or U is 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 β€” true if T is swappable with U, false otherwise. it's a [static] public static member constant ✨

🌸 MEMBER FUNCTIONS 🌸

  • operator bool β€” converts the object to bool, returns value (public member function πŸ‘‘)
  • operator() β€” returns value (C++14) (public member function 🌈)

πŸ“ MEMBER TYPES πŸ“

  • value_type β€” defined as bool
  • type β€” defined as std::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