File: n3018.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (87 lines) | stat: -rw-r--r-- 3,026 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// RUN: %clang_cc1 -std=c23 -verify -triple x86_64 -pedantic -Wno-conversion -Wno-constant-conversion %s

/* WG14 N3018: Full
 * The constexpr specifier for object definitions
 */

#define ULLONG_MAX (__LONG_LONG_MAX__*2ULL+1ULL)
#define UINT_MAX  (__INT_MAX__  *2U +1U)

void Example0() {
  constexpr unsigned int minusOne    = -1;
  // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned int'}}
  constexpr unsigned int uint_max    = -1U;
  constexpr double onethird          = 1.0/3.0;
  constexpr double onethirdtrunc     = (double)(1.0/3.0);

  constexpr char string[] = { "\xFF", };
  constexpr unsigned char ucstring[] = { "\xFF", };
  // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned char'}}
  constexpr char string1[] = { -1, 0, };
  constexpr unsigned char ucstring1[] = { -1, 0, };
  // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned char'}}

  // TODO: Make sure these work correctly once char8_t and _Decimal are supported
  // constexpr char8_t u8string[] = { 255, 0, }; // ok
  // constexpr char8_t u8string[]       = { u8"\xFF", };     // ok
  // constexpr _Decimal32 small         = DEC64_TRUE_MIN * 0;// constraint violation
}

void Example1() {
  constexpr int K = 47;
  enum {
      A = K,
  };
  constexpr int L = K;
  static int b    = K + 1;
  int array[K];
  _Static_assert(K == 47);
}

constexpr int K = 47;
static const int b = K + 1;

void Example2() {
  constexpr int A          = 42LL;
  constexpr signed short B = ULLONG_MAX;
  // expected-error@-1 {{constexpr initializer evaluates to 18446744073709551615 which is not exactly representable in type 'const short'}}
  constexpr float C        = 47u;

  constexpr float D = 432000000;
  constexpr float E = 1.0 / 3.0;
  // expected-error@-1 {{constexpr initializer evaluates to 3.333333e-01 which is not exactly representable in type 'const float'}}
  constexpr float F = 1.0f / 3.0f;
}


void Example3() {
  constexpr static unsigned short array[] = {
      3000,
      300000,
      // expected-error@-1 {{constexpr initializer evaluates to 300000 which is not exactly representable in type 'const unsigned short'}}
      -1
      // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned short'}}
  };

  constexpr static unsigned short array1[] = {
      3000,
      3000,
      -1
       // expected-error@-1 {{constexpr initializer evaluates to -1 which is not exactly representable in type 'const unsigned short'}}
  };

  struct S {
      int x, y;
  };
  constexpr struct S s = {
      .x = __INT_MAX__,
      .y = UINT_MAX,
      // expected-error@-1 {{constexpr initializer evaluates to 4294967295 which is not exactly representable in type 'int'}}
  };
}

void Example4() {
  struct s { void *p; };
  constexpr struct s A = { nullptr };
  constexpr struct s B = A;
}