[libcxx-commits] [libcxx] [libc++] Annotate the data member of variant with no_unique_address (PR #137783)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Apr 29 04:50:34 PDT 2025
================
@@ -84,5 +100,8 @@ int main(int, char**) {
static_assert(sizeof(std::variant<char, int, long>) == sizeof(type_with_index<long>));
static_assert(sizeof(std::variant<std::size_t, std::size_t, std::size_t>) == sizeof(type_with_index<std::size_t>));
+ static_assert((sizeof(VariantWithNoUniqueAddress) < sizeof(VariantWithoutNoUniqueAddress)) ==
+ ExpectSmallerSizeWithNoUniqueAddress);
+
----------------
frederick-vs-ja wrote:
I think we should ensure that the following still works - i.e. `b` won't be accidentally overwritten.
At least we should add this to the test suite (into `libcxx/test/std` subdirectory).
```C++
#include <cassert>
#include <variant>
#include "test_macros.h"
struct S {
[[no_unique_address]] std::variant<int, void*> a;
bool b;
};
TEST_CONSTEXPR_CXX20 bool test() {
S x{{}, true};
x.a.emplace<0>();
x.a.emplace<1>();
return x.b;
}
#if TEST_STD_VER >= 20
static_assert(test());
#endif
int main() {
assert(test());
}
```
https://github.com/llvm/llvm-project/pull/137783
More information about the libcxx-commits
mailing list