https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121637

            Bug ID: 121637
           Summary: Anonymous structs with GCC are broken
           Product: gcc
           Version: 15.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nikolasklauser at berlin dot de
  Target Milestone: ---

GCC currently produces garbage code with an anonymous struct that contains a
type which is not trivially default constructible or destructible inside a
template. Without the template, GCC rejects the code.
```
struct S {
  S();
  ~S();
};

template <class S>
struct T {
  struct {
    S s;
  };
};

auto inst() {
  return T<S>();
}
```
This is accepted by GCC, but inst() never calls S().

The obvious fix here is to reject this code in templates just like it would be
rejected in non-template cases. There are multiple reasons GCC should always
accept this code and generate correct machine code instead:
(1) Clang and MSVC already accept this code, making GCC the only compiler
rejecting such structs
(2) anonymous structs allow users to have more control over where subobjects
marked with [[no_unique_address]] are shifted to, potentially improving the
layout of certain types
(3) This is the only solution we've found that fixes an ABI break for libc++.
Specifically, we've tried switching to [[no_unique_address]] from our internal
`__compressed_pair`. This has been done a while ago and would be non-trivial to
revert. For this reason, we've decided to keep the ABI break for GCC. This was
not an easy decision for us, and we hope that GCC will implement anonymous
structs with non-trivial types so that we can fix the ABI for GCC as well. See
https://github.com/llvm/llvm-project/pull/154686 and
https://github.com/llvm/llvm-project/issues/154146 for more details.
  • [Bug c++/121637] New: Anonymo... nikolasklauser at berlin dot de via Gcc-bugs

Reply via email to