C++ Logo

STD-PROPOSALS

Advanced search

Subject: [std-proposals] Proposal: no_destructor template
From: Walt Karas (wkaras_at_[hidden])
Date: 2019-08-13 12:04:04


One approach to the static initialization order problem looks like:

A & a1()
{
static A a;
return(a);
}

The problem is that, if A had a non-trivial destructor, the static order destruction problem could still cause undefined behavior.  A solution to this, avoiding the call to A::~A, is:

A & a2()
{
static A* a(new A);
return(*a);
}

This template, no_destructor,  avoids the destructor call without requiring an unnecessary dynamic allocation:

#include <utility>
#include <new>

template <typename T>
class no_destructor
{
public:

template <typename ... Args>
no_destructor(Args && ... args)
{
new (payload) T(std::forward<Args>(args)...);
}

T & operator () () { return(*reinterpret_cast<T *>(payload)); }

private:

alignas(T) char payload[sizeof(T)];
};

A & a3()
{
static no_destructor<A> a;
return(a());
}

https://godbolt.org/z/cGSXRy


STD-PROPOSALS list run by herb.sutter at gmail.com

Standard Proposals Archives on Google Groups