File: constexpr-cast.C

package info (click to toggle)
gcc-arm-none-eabi 15%3A8-2019-q3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 571,828 kB
  • sloc: ansic: 2,937,651; cpp: 881,644; ada: 597,189; makefile: 65,528; asm: 56,499; xml: 46,621; exp: 24,747; sh: 19,684; python: 7,256; pascal: 4,370; awk: 3,497; perl: 2,695; yacc: 316; ml: 285; f90: 234; lex: 198; objc: 194; haskell: 119
file content (24 lines) | stat: -rw-r--r-- 857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Test to verify that evaluating reinterpret_cast is diagnosed in
// constant expressions.
// { dg-do compile { target c++11 } }

int i;

// The following was accepted due to bug 49171.
constexpr void *q = reinterpret_cast<void*>(&i);    // { dg-error "not a constant expression" }

constexpr void *r0 = reinterpret_cast<void*>(1);    // { dg-error "not a constant expression|reinterpret_cast from integer to pointer" }
constexpr void *r1 = reinterpret_cast<void*>(sizeof 'x');  // { dg-error ".reinterpret_cast<void\\*>\\(1\[ul\]\*\\). is not a constant expression" }

template <class T>
constexpr bool f ()
{
#if __cplusplus > 201103L
  T *p = reinterpret_cast<T*>(sizeof (T));
  return p;
#else
  return *reinterpret_cast<T*>(sizeof (T));
#endif
}

constexpr bool b = f<int>();   // { dg-error "not a constant expression|in .constexpr. expansion of " }