1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___TYPE_TRAITS_DATASIZEOF_H 10 #define _LIBCPP___TYPE_TRAITS_DATASIZEOF_H 11 12 #include <__config> 13 #include <__cstddef/size_t.h> 14 15 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 16 # pragma GCC system_header 17 #endif 18 19 // This trait provides the size of a type excluding any tail padding. 20 // 21 // It is useful in contexts where performing an operation using the full size of the class (including padding) may 22 // have unintended side effects, such as overwriting a derived class' member when writing the tail padding of a class 23 // through a pointer-to-base. 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 // TODO: Enable this again once #94816 is fixed. 28 #if (__has_keyword(__datasizeof) || __has_extension(datasizeof)) && 0 29 template <class _Tp> 30 inline const size_t __datasizeof_v = __datasizeof(_Tp); 31 #else 32 template <class _Tp> 33 struct _FirstPaddingByte { 34 _LIBCPP_NO_UNIQUE_ADDRESS _Tp __v_; 35 char __first_padding_byte_; 36 }; 37 38 // _FirstPaddingByte<> is sometimes non-standard layout. 39 // It is conditionally-supported to use __builtin_offsetof in that case, but GCC and Clang allow it. 40 _LIBCPP_DIAGNOSTIC_PUSH 41 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof") 42 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Winvalid-offsetof") 43 template <class _Tp> 44 inline const size_t __datasizeof_v = __builtin_offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_); 45 _LIBCPP_DIAGNOSTIC_POP 46 #endif // __has_extension(datasizeof) 47 48 _LIBCPP_END_NAMESPACE_STD 49 50 #endif // _LIBCPP___TYPE_TRAITS_DATASIZEOF_H 51