xref: /aosp_15_r20/external/webrtc/rtc_base/system/no_unique_address.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef RTC_BASE_SYSTEM_NO_UNIQUE_ADDRESS_H_
12 #define RTC_BASE_SYSTEM_NO_UNIQUE_ADDRESS_H_
13 
14 // RTC_NO_UNIQUE_ADDRESS is a portable annotation to tell the compiler that
15 // a data member need not have an address distinct from all other non-static
16 // data members of its class.
17 // It allows empty types to actually occupy zero bytes as class members,
18 // instead of occupying at least one byte just so that they get their own
19 // address. There is almost never any reason not to use it on class members
20 // that could possibly be empty.
21 // The macro expands to [[no_unique_address]] if the compiler supports the
22 // attribute, it expands to nothing otherwise.
23 // Clang should supports this attribute since C++11, while other compilers
24 // should add support for it starting from C++20. Among clang compilers,
25 // clang-cl doesn't support it yet and support is unclear also when the target
26 // platform is iOS.
27 #ifndef __has_cpp_attribute
28 #define __has_cpp_attribute(__x) 0
29 #endif
30 #if __has_cpp_attribute(no_unique_address)
31 // NOLINTNEXTLINE(whitespace/braces)
32 #define RTC_NO_UNIQUE_ADDRESS [[no_unique_address]]
33 #else
34 #define RTC_NO_UNIQUE_ADDRESS
35 #endif
36 
37 #endif  // RTC_BASE_SYSTEM_NO_UNIQUE_ADDRESS_H_
38