std::is_des...estructible(3)

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

πŸ’– std::is_des...estructible(3) πŸ’–

Okay bestie, this page is straight from the C++ Standard Libary (yes, that's how it's spelled on the page, we are not fixing it πŸ’…) and it's all about the destructible-checking trio. The NAME section is serving std::is_destructible,std::is_trivially_destructible,std::is_nothrow_destructible β€” that's the whole guest list, no cap.


🌸 Synopsis 🌸

Defined in header <type_traits>
template< class T >               (1) (since C++11)
struct is_destructible;
template< class T >               (2) (since C++11)
struct is_trivially_destructible;
template< class T >               (3) (since C++11)
struct is_nothrow_destructible;
  1. If T is a reference type, it provides the member constant value equal to true. If T is a (possibly cv-qualified) void, a function type, or an array of unknown bound, value equals false. If T is an object type, then, for the type U that is std::remove_all_extents<T>::type, if the expression std::declval<U&>().~U() is well-formed in unevaluated context, value equals true. Otherwise, value equals false.
  2. Same as (1), and additionally std::remove_all_extents<T>::type is either a non-class type or a class type with a trivial destructor.
  3. Same as (1), but the destructor is noexcept.
⚠️ Undefined behavior alert, bestie: 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, the behavior is undefined.
⚠️ And one more, fr fr: 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 destructible, false otherwise. It's [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>

🌈 Notes 🌈

  • Because the C++ program terminates if a destructor throws an exception during stack unwinding (which usually cannot be predicted), all practical destructors are non-throwing even if they are not declared noexcept. All destructors found in the C++ standard library are non-throwing. Iconic behavior, honestly.
  • Storage occupied by trivially destructible objects may be reused without calling the destructor.

πŸ§ͺ Example πŸ§ͺ

// Run this code πŸƒβ€β™€οΈπŸ’¨

#include <iostream>
#include <string>
#include <type_traits>

struct Foo
{
    std::string str;
    ~Foo() noexcept {};
};

struct Bar
{
    ~Bar() = default;
};

int main()
{
    std::cout << std::boolalpha
              << "std::string is destructible? "
              << std::is_destructible<std::string>::value << '\n'
              << "Foo is trivially destructible? "
              << std::is_trivially_destructible_v<Foo> << '\n'
              << "Foo is nothrow destructible? "
              << std::is_nothrow_destructible<Foo>() << '\n'
              << "Bar is trivially destructible? "
              << std::is_trivially_destructible<Bar>{} << '\n';
}

πŸŽ€ Output πŸŽ€

std::string is destructible? true
Foo is trivially destructible? false
Foo is nothrow destructible? true
Bar is trivially destructible? true

🩹 Defect reports 🩹

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 2049 β€” Applied to: C++11 β€” Behavior as published: the specification was incompletable because of the imaginary wrapping struct. Correct behavior: made complete.

πŸ‘€ See also πŸ‘€

  • is_constructible
  • is_trivially_constructible
  • is_nothrow_constructible β€” checks if a type has a constructor for specific arguments; markers: (C++11), (class template), (C++11), (C++11).
  • has_virtual_destructor β€” checks if a type has a virtual destructor; (C++11), (class template).
  • destructible β€” specifies that an object of the type can be destroyed; (C++20), (concept).
  • destructor β€” releases claimed resources.

Source: http://cppreference.com ✨ Version/date: 2024.06.10 ✨ Page: std::is_des...estructible(3) β€” bye bestie, go check those destructors πŸ«ΆπŸ’–

πŸ“¦ where this page comes from