Skip to content

Clang and GCC disagree on whether lambda closure type is "POD for the purpose of layout" #59078

@hubert-reinterpretcast

Description

@hubert-reinterpretcast

Similar to #46056, Clang and GCC disagree on "POD for the purpose of layout": in this case, on closure types.

It appears that, in the case below, GCC considers the first closure type to be "POD for the purpose of layout" and does not allocate additional subobjects of the derived class into the tail padding. Clang does allocate into the tail padding, resulting in a smaller size of 8 for the derived class instead of 12.

https://godbolt.org/z/4T8f1sbMr

SOURCE (<stdin>):

template <typename ...T>
struct Callables : T ... {
  Callables(T ...t) : T(t) ... {}
  using T::operator() ...;
};

inline auto f(int x, char y) {
  Callables callables([x, y](void) {}, [y](int) {});
  static_assert(sizeof(callables) == 12);
  return callables;
}

int main(void) { f(0, 1)(42); }

COMPILER INVOCATION:

clang++ --target=x86_64-unknown-linux-gnu -xc++ -fsyntax-only -

ACTUAL OUTPUT:

<stdin>:9:3: error: static assertion failed due to requirement 'sizeof (callables) == 12'
  static_assert(sizeof(callables) == 12);
  ^             ~~~~~~~~~~~~~~~~~~~~~~~
<stdin>:9:35: note: expression evaluates to '8 == 12'
  static_assert(sizeof(callables) == 12);
                ~~~~~~~~~~~~~~~~~~^~~~~
1 error generated.

EXPECTED OUTPUT:

Clean compile.

COMPILER VERSION INFO (clang++ -v):

clang version 16.0.0 (https://github.com/llvm/llvm-project.git a72d8d704178118b254d9ff84a78afb18813b888)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/wandbox/clang-head/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Candidate multilib: .;@m64
Selected multilib: .;@m64

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions