[clang] [Clang] Implement CWG2598: Union of non-literal types (PR #78195)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 17 06:40:49 PST 2024


groundswellaudio wrote:

Thanks for the fix @cor3ntin. There is still a problem however with `std::optional`, which I think boils down to implicit destructor and anonymous union : 

```cpp
struct A {
  A ();
  ~A();
};

template <class T>
struct opt
{
  union {
    char c;
    T data;
  };
  
  constexpr opt() {}
  
  constexpr ~opt() {
    if (engaged)
      data.~T();
  }
  
  bool engaged = false;
};

consteval void foo()
{
  opt<A> a;
}
```

This fails with `non-literal type 'opt<A>' cannot be used in a constant expression`, but compiles if A has no destructor (or if that destructor is constexpr). 

I suspect that the culprit is here : https://github.com/llvm/llvm-project/blob/fca6992be1f272f5e997bd510ca03c9389550c13/clang/lib/AST/DeclCXX.cpp#L550

The implicit destructor of the anonymous union is not constexpr because one of its subobjects has a non-constexpr destructor. 

https://github.com/llvm/llvm-project/pull/78195


More information about the cfe-commits mailing list