std::is_cop...assignable,(3)

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

πŸ¦‹ std::is_cop...assignable,(3) πŸ¦‹

okay bestie, this is the C++ Standard Libary page for the copy-assignable type traits β€” yes, the header really spells it β€œLibary” 😭. We’re talking about std::is_copy_assignable, std::is_trivially_copy_assignable, and the nothrow one too. The page’s own manual name is std::is_cop...assignable,(3), so that’s the vibe. πŸ’…

πŸ’– NAME πŸ’–

The NAME line is literally:

std::is_copy_assignable,std::is_trivially_copy_assignable, - std::is_copy_assignable,std::is_trivially_copy_assignable,

yes, it repeats itself and leaves out the nothrow trait in that line β€” that’s just what the page says, bestie. 🫢

🌸 Synopsis 🌸

Defined in header <type_traits>.

template< class T >                  (1) (since C++11)
struct is_copy_assignable;
template< class T >                  (2) (since C++11)
struct is_trivially_copy_assignable;
template< class T >                  (3) (since C++11)
struct is_nothrow_copy_assignable;

The value of the member constant value is:

  • if T is a referenceable type:
    • (1) β†’ std::is_assignable<T, const T&>::value
    • (2) β†’ std::is_trivially_assignable<T, const T&>::value
    • (3) β†’ std::is_nothrow_assignable<T, const T&>::value
  • if T is not a referenceable type β†’ false πŸ’€
🚨 undefined behavior alert: if T is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined. 🚨 also 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. 🚨 and if the program adds specializations for any of the templates described on this page, the behavior is undefined.

πŸ‘‘ Member constants πŸ‘‘

  • value β€” true if T is copy-assignable, false otherwise; [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 β€” bool
  • type β€” std::integral_constant<bool, value>

🌈 Possible implementation 🌈

        template<class T>
        struct is_copy_assignable
            : std::is_assignable<typename std::add_lvalue_reference<T>::type,
                                 typename std::add_lvalue_reference<const
       T>::type> {};

        template<class T>
        struct is_trivially_copy_assignable
            : std::is_trivially_assignable<typename
       std::add_lvalue_reference<T>::type,
                                           typename
       std::add_lvalue_reference<const T>::type> {};

        template<class T>
        struct is_nothrow_copy_assignable
            : std::is_nothrow_assignable<typename
       std::add_lvalue_reference<T>::type,
                                         typename
       std::add_lvalue_reference<const T>::type> {};

πŸ’œ Notes πŸ’œ

The trait std::is_copy_assignable is less strict than CopyAssignable because it does not check the type of the result of the assignment (which, for a CopyAssignable type, must be an lvalue of type T) and does not check the semantic requirement that the argument expression remains unchanged. It also does not check that T satisfies MoveAssignable, which is required of all CopyAssignable types.

so like… it’s not doing the full vibe check, just the assignment part. πŸ’…

πŸ“ Example πŸ“

The page says // Run this code before the snippet:

#include <iostream>
#include <type_traits>
#include <utility>

struct Foo { int n; };

int main()
{
    std::cout << std::boolalpha
              << "Foo is trivially copy-assignable? "
              << std::is_trivially_copy_assignable<Foo>::value << '\n'
              << "int[2] is copy-assignable? "
              << std::is_copy_assignable<int[2]>::value << '\n'
              << "int is nothrow copy-assignable? "
              << std::is_nothrow_copy_assignable<int>::value << '\n';
}

πŸ“ Output πŸ“

Foo is trivially copy-assignable? true
int[2] is copy-assignable? false
int is nothrow copy-assignable? true

πŸ¦‹ See also πŸ¦‹

  • is_assignable / is_trivially_assignable / is_nothrow_assignable β€” checks if a type has an assignment operator for a specific argument (class template); all three are (C++11) ✨
  • is_move_assignable / is_trivially_move_assignable / is_nothrow_move_assignable β€” checks if a type has a move assignment operator (class template); all three are (C++11) ✨

okay that’s the whole page, bestie 🫢 Source: http://cppreference.com Β· Version/date: 2024.06.10 Β· Manual: std::is_cop...assignable,(3) πŸ’…βœ¨

πŸ“¦ where this page comes from