Skip to content

Commit fa925d7

Browse files
committed
Document C++ compatibility hazard
1 parent bc72ad6 commit fa925d7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Diff for: reference/src/layout/unions.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ assert_eq!(size_of::<U>(), 1);
8787
# }
8888
```
8989

90-
The field does, however, participate in the layout computation of the union. For
91-
example:
90+
The field does, however, participate in the layout computation of the union, and can raise its alignment requirement, which in turn can introduce trailing padding. For example:
9291

9392
```rust
9493
# use std::mem::{size_of, align_of};
@@ -103,8 +102,12 @@ assert_eq!(align_of::<U>(), 2);
103102
// This introduces trailing padding, raising the union size to 2
104103
assert_eq!(size_of::<U>(), 2);
105104
# }
106-
```
105+
``**
107106

108107
This handling of zero-sized types is equivalent to the handling of zero-sized
109108
types in struct fields, and matches the behavior of GCC and Clang for unions in
110109
C when zero-sized types are allowed via their language extensions.
110+
111+
**C++ compatibility hazard**: C++ does, in general, give a size of 1 to empty
112+
structs. If an empty struct in C++ is used as an union field, a "naive"
113+
translation of that code into Rust will not produce a compatible result.

0 commit comments

Comments
 (0)