[libcxx-commits] [libcxx] [libc++] Ensure that `std::expected` has no tail padding (PR #69673)

Jan Kokemüller via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 19 20:20:54 PDT 2023


jiixyj wrote:

GNU anonymous struct members seem to work great for this use case:

```c++
#include <memory>

//

struct bp {
   private:
    alignas(4) bool b_;
};
static_assert(sizeof(bp) == 4);
static_assert(std::__libcpp_datasizeof<bp>::value == 1);

//

struct s1 {
    s1() : bp_{}, b_{} {};

   private:
    [[no_unique_address]] bp bp_;
    bool b_;
};
static_assert(sizeof(s1) == 4);
static_assert(std::__libcpp_datasizeof<s1>::value == 2);  // bad :(

//

struct s2 {
    s2() : bp_{}, b_{} {};

   private:
    struct {
        [[no_unique_address]] bp bp_;
        bool b_;
    };
};
static_assert(sizeof(s2) == 4);
static_assert(std::__libcpp_datasizeof<s2>::value == 4);  // good :)
```
<https://godbolt.org/z/EenY54E4r>

Sadly this is not portable...

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


More information about the libcxx-commits mailing list