1 // Test for std::byte aliasing properties.
2 // { dg-options "-std=c++17 -O3" }
3 
4 #include <cstddef>
5 
6 using byte = std::byte;
7 
8 enum class notbyte: unsigned char {} *np;
9 
main()10 int main()
11 {
12   int x;
13 
14   /* Stores through byte* can alias int, so the compiler can't optimize
15      "x != 0".  */
16   byte *p = (byte*)&x;
17   x = 42;
18   for (int i = 0; i < 4; ++i)
19     p[i] = byte(0);
20   if (x != 0)
21     __builtin_abort();
22 
23   /* Stores through notbyte* mustn't alias int, so at -O3 the compiler should
24      optimize "x != 42" to false.  */
25   notbyte *np = (notbyte*)&x;
26   x = 42;
27   for (int i = 0; i < 4; ++i)
28     np[i] = notbyte(0);
29   if (x != 42)
30     __builtin_abort();
31 }
32