https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61973
Bug ID: 61973
Summary: __thread and deleted destructor
Product: gcc
Version: 4.8.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: nyh at math dot technion.ac.il
Hi, right now "__thread" insists that the given type has a trivial destructor.
There is unfortunately no way to silence this error, even when I deliberately
want this (e.g., I know I'll call ~T() explicitly in another way).
I figured out a way (see below) to *delete* a type's destructor, using a union
(see below). However, __thread only accepts a trivial destructor, not a deleted
destructor. Is there a reason __thread cannot accept a type with a deleted
destructor?
Bonus points to anyone who can figure out how I can use __thread (not
thread_local) on a type with a non-trivial destructor even in existing gcc ;-)
The code to "delete" a type's destructor:
template <class T>
class mask_destructor {
private:
union {
alignas(T) char bytes[sizeof(T)];
T obj;
};
public:
T& get() const { return obj; }
// preserve T's constexpr constructor
constexpr mask_destructor() : obj() { }
mask_destructor(const mask_destructor &other) : obj(other.obj) { }
mask_destructor &operator=(const mask_destructor &other){
obj = other.obj;
return *this;
}
// Note no destructor defined. The compile implicitly deletes it
// because of the anonymous union above.
};