<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/146879>146879</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang++] Error when inspect the common initial sequence of standard-layout structs of the union
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
safocl
</td>
</tr>
</table>
<pre>
Code
```cpp
consteval void foo(){
union { struct {int i1; int i2; int i3; int i4;} s;
struct {int i1; int i2; int i3; int i4;} s2;
int i[4];
} u{};
auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
}
int main(){
foo();
}
```
produces error with
```
note: read of member 's2' of union with active member 's' is not allowed in a constant expression
7 | auto sum = u.s2.i1+u.s2.i1+u.s2.i1+u.s2.i1;
| ^
```
https://godbolt.org/z/dcPWW8ncx
(this tested from clang-x86_64 10.0.0 to clang-x86_64 20.1.0 and trunk version on godbolt)
but:
https://eel.is/c++draft/class.union#general-note-1
https://eel.is/c++draft/class.mem#general-27
https://eel.is/c++draft/class.prop#10
https://eel.is/c++draft/class.mem#def:layout-compatible,class
the `s` and `s2` union members are two standard-layout struct types and have common initial sequence (and what's more: they are layout-compatible).
in my opinion the error behavior here is a bug.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVMGSozgM_RrlogplBInJgUPSmZz3NsctAyJ419isbdKd_fotQ3qmp2dnqrpTqUKY92T56ckqBH21zDXsTrA7b9QcB-froHrXmk3junv95DoGcYS9WP_tNIE4ts6GyDdl8OZ0h71zQBXQAeQJxBERcbbaWQR5whD93MYUahtR51CccInoW1R8i0ooTiDPGNJTHD9Jpgf7zW8BwO5Uwu78-Jqgc6pYnn_Gp99jSc3RYZhHhOKMcxYo0znQ6XfRki7lFUcQx7T3qLT9QaPvmr0Fv8q8EifvurnlgOy98_is44DvUNZFhuKInlWHrseRx4Y9AslAQDItra1YyKqN-sZvQQmjA1oXURnjnrlDbVHh0mFlI_LL5DkE7exDDokgnz4tykNZ-YSw-_LziYcYpwDFEegCdLm6rnEmZs5fgS7_Al269o-vXyvbviQ4VXHQASOHyB323o3YGmWv25dq_-e-xFxkIhMY3Y_LJLI8E6hsh9HP9m-8sU_nQ2fxsWNqy1JPM8dUzfvCmE2mA9ClBToBnTqv-phejQohWwQHKq5s2SuzTT3a5h9MMvL4JgXJD9In7yagIhef2rbjHoqjUXc3x23rxklF3RgGelpQqzZxYIS9CLBfxUwxpZfVcKvJAirPGJ8dJjt1ynfbNe3raMf7xGHhD-rG2LpxdBa11VErg4H_mdm2jEBVwjwPKibb4uj84vs48H3Z4n-KPWSv84fjHd2kl7pS2es8NTyom3YeB_acpkBhM1-zTVcX3aE4qA3Xudzlu7LKSWyGuiTue-qLfSkrySVLKdtDS4dettWB9v1G1yRoJ6Qo8kJUpcgqWVKvuCtlKw6keigFj0qbzJjbmGy90SHMXOflvpKHjVENm7DcxkSLZ4EoXcy-ToRtM18DlMLoEMP3FFFHs1zhD0ZqKuzO-GW9NAZOcoaJk9jDrxV2_S9aFNKnxFz6upm9qd-NqY7D3GStG4EuqazHYzt59xe3yVzLMZPpHie91fRfAAAA__8hYfhi">