view gcc/testsuite/g++.dg/cpp1z/byte1.C @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children
line wrap: on
line source

// Test for std::byte aliasing properties.
// { dg-do compile { target c++17 } }
// { dg-options "-O3" }

#include <cstddef>

using byte = std::byte;

enum class notbyte: unsigned char {} *np;

int main()
{
  int x;

  /* Stores through byte* can alias int, so the compiler can't optimize
     "x != 0".  */
  byte *p = (byte*)&x;
  x = 42;
  for (int i = 0; i < 4; ++i)
    p[i] = byte(0);
  if (x != 0)
    __builtin_abort();

  /* Stores through notbyte* mustn't alias int, so at -O3 the compiler should
     optimize "x != 42" to false.  */
  notbyte *np = (notbyte*)&x; 
  x = 42;
  for (int i = 0; i < 4; ++i)
    np[i] = notbyte(0);
  if (x != 42)
    __builtin_abort();
}