File tree 3 files changed +26
-1
lines changed
test/libcxx/utilities/variant/variant.variant
3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 57
57
// Use the smallest possible integer type to represent the index of the variant.
58
58
// Previously libc++ used "unsigned int" exclusively.
59
59
# define _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
60
+ // Allow to reuse tail padding after the index of the variant with [[no_unique_address]] attribute.
61
+ # define _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
60
62
// Unstable attempt to provide a more optimized std::function
61
63
# define _LIBCPP_ABI_OPTIMIZED_FUNCTION
62
64
// All the regex constants must be distinct and nonzero.
Original file line number Diff line number Diff line change @@ -1319,7 +1319,11 @@ public:
1319
1319
# endif
1320
1320
1321
1321
private:
1322
- __variant_detail::__impl<_Types...> __impl_;
1322
+ # ifdef _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
1323
+ _LIBCPP_NO_UNIQUE_ADDRESS
1324
+ # endif // _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
1325
+ __variant_detail::__impl<_Types...>
1326
+ __impl_;
1323
1327
1324
1328
friend struct __variant_detail ::__access::__variant;
1325
1329
friend struct __variant_detail ::__visitation::__variant;
Original file line number Diff line number Diff line change @@ -70,6 +70,22 @@ struct type_with_index {
70
70
#endif
71
71
};
72
72
73
+ struct alignas (16 ) A16 {};
74
+ struct VariantWithNoUniqueAddress {
75
+ TEST_NO_UNIQUE_ADDRESS std::variant<A16> a;
76
+ bool b;
77
+ };
78
+ struct VariantWithoutNoUniqueAddress {
79
+ std::variant<A16> a;
80
+ bool b;
81
+ };
82
+ constexpr bool ExpectSmallerSizeWithNoUniqueAddress =
83
+ #ifdef _LIBCPP_ABI_VARIANT_NO_UNIQUE_ADDRESS_OPTIMIZATION
84
+ true ;
85
+ #else
86
+ false ;
87
+ #endif
88
+
73
89
int main (int , char **) {
74
90
test_index_type<unsigned char >();
75
91
// This won't compile due to template depth issues.
@@ -84,5 +100,8 @@ int main(int, char**) {
84
100
static_assert (sizeof (std::variant<char , int , long >) == sizeof (type_with_index<long >));
85
101
static_assert (sizeof (std::variant<std::size_t , std::size_t , std::size_t >) == sizeof (type_with_index<std::size_t >));
86
102
103
+ static_assert ((sizeof (VariantWithNoUniqueAddress) < sizeof (VariantWithoutNoUniqueAddress)) ==
104
+ ExpectSmallerSizeWithNoUniqueAddress);
105
+
87
106
return 0 ;
88
107
}
You can’t perform that action at this time.
0 commit comments