std::assignable_from(3)

πŸ“¦
version 2024.07.05 Β· 3 versions
, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

std::assignable_from β€” bestie, it's the concept that checks whether you can assign one thing to another πŸ’…


✨ SYNOPSIS ✨

Okay so first things first, this lives in the header <concepts> β€” that's the include you want πŸŽ€

template< class LHS, class RHS >
concept assignable_from =
    std::is_lvalue_reference_v<LHS> &&
    std::common_reference_with<
        const std::remove_reference_t<LHS>&,
        const std::remove_reference_t<RHS>&> &&
    requires(LHS lhs, RHS&& rhs) {
        { lhs = std::forward<RHS>(rhs) } -> std::same_as<LHS>;
    };

(since C++20) πŸ’–


πŸ’… DESCRIPTION πŸ’…

So like, assignable_from<LHS, RHS> is the concept that says: an expression of the type and value category that RHS specifies can be assigned to an lvalue expression whose type is LHS. That's it, that's the vibe ✨

Like, lhs = std::forward<RHS>(rhs) has to actually work and hand you back an LHS, no cap 🫢


πŸ’€ NOTES πŸ’€

Not me about to tell you the most important part 😭 Assignment does not have to be a total function. Translation for the girlies: sometimes x = y just isn't valid, and that's fine, it's not a bug.

Specifically β€” if assigning to some object x can cause some other object y to get modified, then x = y is likely not in the domain of =. πŸ™ˆ

When does that happen? Typically when the right operand is owned, directly or indirectly, by the left operand. For example: smart pointers to nodes in a node-based data structure, or something like std::vector<std::any>. Iconic, but also a little cursed ngl πŸ”₯


πŸ‘€ SEE ALSO πŸ‘€

  • std::is_assignable β€” checks if a type has an assignment operator for a specific argument (class template, C++11) πŸ’œ
  • std::is_trivially_assignable β€” same job, checking if a type has an assignment operator for a specific argument (class template, C++11) πŸ¦‹
  • std::is_nothrow_assignable β€” again with the "checks if a type has an assignment operator for a specific argument" energy (class template, C++11) 🌈

Ok that's the whole page bestie, go assign some things πŸŽ€ source vibes straight from http://cppreference.com, version 2024.06.10 β€” mwah, slay, periodt πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from