A sum type such as std::optional is implemented with conditionally trivial member functions because the template parameter may have a non trivial destructor. It's destructor is thus implemented as follows:
~optional()
{
if (bool(*this))
{
payload.~T();
}
}
~optional() requires std::is_trivially_destructible_v<T> = default;
And to express a disengaged state we write:
constexpr optional& operator = (nullopt_t) noexcept
{
if (bool(*this))
{
payload.~stored_type();
engaged = false;
}
return *this;
}
But what if our sum type's template parameter is restricted to being trivially destructible...
template <std::integral T>
class trivial_sum_type;
How do we ensure, for instance within a swap member function, that when we exchange values from an engaged to a disengaged object that the destructor for the value within the previously engaged object is called? And vice versa
template <std::integral T>
class trivial_sum_type
{
//everything else same as std::optional except it is restriced to a trivial type i.e std::integral
constexpr void swap(const trivial_sum_type& rhs)
{
if (bool(*this) and bool(rhs))
{
std::swap(payload, *rhs);
}
else if (bool(*this) and not bool(rhs))
{
new(std::addressof(payload))stored_type(std::move(payload));
payload.~stored_type(); //what if stored_type is an int
}
else if (not bool(*this) and bool(rhs))
{
new(std::addressof(payload))stored_type(std::move(*rhs));
rhs.payload.~stored_type(); //same issue here
}
}
~trivial_sum_type() = default;
private:
struct empty_byte{};
union
{
T payload;
empty_byte empty;
}
bool engaged;
}
My confusion is here: you do not explicitly write a destructor for a trivially destructible type within the sum type's destructor function, but does it also mean that when you swap or express a disengaged state you should not call a destructor on such types. Which leads me to the question do trivial types have destructors? And how do you destroy the value of a trivially destructible type within a disengaged trivially destructible sum type? Hopefully...am not stack raving mad and my lack of understanding makes sense