Search⌘ K
AI Features

Multiple Destructors with C++20

Explore how to use C++20 concepts and requires clauses to create multiple destructors in class templates. Understand how constraints affect destructor selection and the current compiler support challenges.

We still have a class template, but instead of std::conditional, we use the trailing requires clause to provide an overload for the destructor.

Remember we learned earlier that, in class templates, we can provide function overloads using different ...

C++
#include <iostream>
#include <string>
#include <type_traits>
template <typename T>
class Wrapper
{
T t;
public:
~Wrapper() requires (!std::is_trivially_destructible_v<T>) {
std::cout << "Not trivial\n";
}
~Wrapper() = default;
};
int main()
{
Wrapper<int> wrappedInt;
Wrapper<std::string> wrappedString;
}

In the ...