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

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

πŸ’– NAME πŸ’–

Okay so this is the page for std::is_copy_constructible, std::is_trivially_copy_constructible, and std::is_nothrow_copy_constructible from the C++ Standard Library β€” the manual label is std::is_cop...structible,(3). The literal NAME line in this copy is: std::is_copy_constructible,std::is_trivially_copy_constructible, - std::is_copy_constructible,std::is_trivially_copy_constructible, (yes bestie, that’s exactly how it’s written πŸ’…).


✨ Synopsis ✨

Defined in header <type_traits>.

template< class T >                     (1) (since C++11)
struct is_copy_constructible;
template< class T >                     (2) (since C++11)
struct is_trivially_copy_constructible;
template< class T >                     (3) (since C++11)
struct is_nothrow_copy_constructible;

The vibe here is all about the value of the member constant value, and the table is giving Type trait, T is a referenceable type, and T is not a referenceable type energy:

  • (1) is_copy_constructible β€” if T is a referenceable type, value is std::is_constructible<T, const T&>::value.
  • (2) is_trivially_copy_constructible β€” if T is a referenceable type, value is std::is_trivially_constructible<T, const T&>::value; if T is not a referenceable type, false.
  • (3) is_nothrow_copy_constructible β€” if T is a referenceable type, value is std::is_nothrow_constructible<T, const T&>::value; if T is not a referenceable type, false.
If T is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined. No cap, that’s the rule 😭
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 copy-constructible, false otherwise; [static] (public static member constant).

πŸ¦‹ Member functions πŸ¦‹

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

🌸 Member types 🌸

  • value_type β€” bool
  • type β€” std::integral_constant<bool, value>

πŸ”₯ Possible implementation πŸ”₯

Here’s the possible implementation, bestie β€” iconic template energy:

template<class T>
struct is_copy_constructible :
    std::is_constructible<T, typename std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};

template<class T>
struct is_trivially_copy_constructible :
    std::is_trivially_constructible<T, typename
std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};

template<class T>
struct is_nothrow_copy_constructible :
    std::is_nothrow_constructible<T, typename
std::add_lvalue_reference<
        typename std::add_const<T>::type>::type> {};

πŸ’œ Notes πŸ’œ

In many implementations, is_nothrow_copy_constructible also checks if the destructor throws because it is effectively noexcept(T(arg)). Same applies to is_trivially_copy_constructible, which, in these implementations, also requires that the destructor is trivial: GCC bug 51452, LWG issue 2116.


🌈 Example 🌈

// Run this code

#include <string>
#include <type_traits>

struct S1
{
    std::string str; // member has a non-trivial copy constructor
};
static_assert(std::is_copy_constructible_v<S1>);
static_assert(!std::is_trivially_copy_constructible_v<S1>);

struct S2
{
    int n;
    S2(const S2&) = default; // trivial and non-throwing
};
static_assert(std::is_trivially_copy_constructible_v<S2>);
static_assert(std::is_nothrow_copy_constructible_v<S2>);

struct S3
{
    S3(const S3&) = delete; // explicitly deleted
};
static_assert(!std::is_copy_constructible_v<S3>);

struct S4
{
    S4(S4&) {}; // can't bind const, hence not a copy-constructible
};
static_assert(!std::is_copy_constructible_v<S4>);

int main() {}

πŸ‘‘ See also πŸ‘‘

  • is_constructible (C++11), is_trivially_constructible (C++11), is_nothrow_constructible (C++11) β€” checks if a type has a constructor for specific arguments (class template).
  • is_default_constructible (C++11), is_trivially_default_constructible (C++11), is_nothrow_default_constructible (C++11) β€” checks if a type has a default constructor (class template).
  • is_move_constructible (C++11), is_trivially_move_constructible (C++11), is_nothrow_move_constructible (C++11) β€” checks if a type can be constructed from an rvalue reference (class template).
  • copy_constructible (C++20) β€” specifies that an object of a type can be copy constructed and move constructed (concept).

That’s the whole page, bestie 🫢 β€” source: http://cppreference.com, dated 2024.06.10, with the page label std::is_cop...structible,(3). Slay ✨

πŸ“¦ where this page comes from