std::is_destructible, std::is_trivially_destructible, std::is_nothrow_destructible

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Endian
(C++20)
Constant evaluation context
Supported operations
is_destructibleis_trivially_destructibleis_nothrow_destructible
(C++11)(C++11)(C++11)
Relationships and property queries
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template< class T >
struct is_destructible;
(1) (since C++11)
template< class T >
struct is_trivially_destructible;
(2) (since C++11)
template< class T >
struct is_nothrow_destructible;
(3) (since C++11)
1) If an imaginary struct containing a member object of type T has a non-deleted destructor, provides the member constant value equal true. For any other type, value is false. (until C++14)

1) If T is a reference type, provides the member constant value equal true

If T is (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 equal 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.

(since C++14)

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.

T shall be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, the behavior is undefined.

Helper variable templates

template< class T >
inline constexpr bool is_destructible_v = is_destructible<T>::value;
(since C++17)
template< class T >
inline constexpr bool is_trivially_destructible_v = is_trivially_destructible<T>::value;
(since C++17)
template< class T >
inline constexpr bool is_nothrow_destructible_v = is_nothrow_destructible<T>::value;
(since C++17)

Inherited from std::integral_constant

Member constants

value
[static]
true if T is destructible, false otherwise
(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

Type Definition
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.

Storage occupied by trivially destructible objects may be reused without calling the destructor.

Example

#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 nothrow destructible? "
              << std::is_nothrow_destructible<Foo>::value << '\n'
              << "Bar is trivally destructible? "
              << std::is_trivially_destructible<Bar>::value << '\n';
}

Output:

std::string is destructible? true
Foo is nothrow destructible? true
Bar is trivally destructible? true

See also

checks if a type has a constructor for specific arguments
(class template)
checks if a type has a virtual destructor
(class template)