Skip to content

Commit eeeb963

Browse files
authored
[libc++] Use __datasizeof for __libcpp_datasizeof if available (#72104)
This avoids the UB and makes things a bit cheaper in terms of compile-times.
1 parent 0f1721c commit eeeb963

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

libcxx/include/__type_traits/datasizeof.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828

2929
template <class _Tp>
3030
struct __libcpp_datasizeof {
31-
#if __has_cpp_attribute(__no_unique_address__)
31+
#if __has_extension(datasizeof)
32+
static const size_t value = __datasizeof(_Tp);
33+
#else
34+
// NOLINTNEXTLINE(readability-redundant-preprocessor) This is https://llvm.org/PR64825
35+
# if __has_cpp_attribute(__no_unique_address__)
3236
template <class = char>
3337
struct _FirstPaddingByte {
3438
[[__no_unique_address__]] _Tp __v_;
3539
char __first_padding_byte_;
3640
};
37-
#else
41+
# else
3842
template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
3943
struct _FirstPaddingByte : _Tp {
4044
char __first_padding_byte_;
@@ -45,14 +49,15 @@ struct __libcpp_datasizeof {
4549
_Tp __v_;
4650
char __first_padding_byte_;
4751
};
48-
#endif
52+
# endif // __has_cpp_attribute(__no_unique_address__)
4953

5054
// _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
5155
// the use as an extension.
5256
_LIBCPP_DIAGNOSTIC_PUSH
5357
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
5458
static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
5559
_LIBCPP_DIAGNOSTIC_POP
60+
#endif // __has_extension(datasizeof)
5661
};
5762

5863
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)