aboutsummaryrefslogtreecommitdiff
path: root/clang/test/CXX/expr/expr.const/p3-0x.cpp
blob: 3eedef3cf7712f929b6f98870b1fb6910d3a5eb4 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify=expected,cxx11 %s
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify=expected,cxx17 %s

// A converted constant expression of type T is a core constant expression,
int nonconst = 8; // expected-note 3 {{here}}
enum NonConstE : unsigned char { NCE = nonconst }; // expected-error {{enumerator value is not a constant expression}} expected-note {{read of non-const}}
template<int = nonconst> struct NonConstT {}; // expected-error {{non-type template argument is not a constant expression}} expected-note {{read of non-const}}
void NonConstF() {
  switch (nonconst) {
    case nonconst: // expected-error {{case value is not a constant expression}} expected-note {{read of non-const}}
      break;
  }
  NonConstT<> V; // expected-note {{while checking a default template argument used here}}
  return;
}

// implicitly converted to a prvalue of type T, where the converted expression
// is a literal constant expression

bool a(int n) {
  constexpr char vowels[] = "aeiou";
  switch (n) {
  case vowels[0]:
  case vowels[1]:
  case vowels[2]:
  case vowels[3]:
  case vowels[4]:
    static_assert(!vowels[5], "unexpected number of vowels");
    return true;
  }
  return false;
}

// and the implicit conversion sequence contains only
//
//  user-defined conversions,
struct S { constexpr operator int() const { return 5; } };
enum E : unsigned char { E5 = S(), E6, E10 = S() * 2, E1 = E5 / 5 };

//  lvalue-to-rvalue conversions,
const E e10 = E10;
template<E> struct T {};
T<e10> s10;

//  integral promotions,
enum class EE { EE32 = ' ', EE65 = 'A', EE1 = (short)1, EE5 = E5 };

//  integral conversions other than narrowing conversions,
int b(unsigned n) {
  switch (n) {
    case E6:
    case EE::EE32: // expected-error {{not implicitly convertible}}
    case (int)EE::EE32:
    case 1000:
    case (long long)1e10: // expected-error {{case value evaluates to 10000000000, which cannot be narrowed to type 'unsigned int'}}
    case -3: // expected-error {{case value evaluates to -3, which cannot be narrowed to type 'unsigned int'}}
      return n;
  }
  return 0;
}
enum class EEE : unsigned short {
  a = E6,
  b = EE::EE32, // expected-error {{not implicitly convertible}}
  c = (int)EE::EE32,
  d = 1000,
  e = 123456, // expected-error {{enumerator value evaluates to 123456, which cannot be narrowed to type 'unsigned short'}}
  f = -3 // expected-error {{enumerator value evaluates to -3, which cannot be narrowed to type 'unsigned short'}}
};
template<unsigned char> using A = int; // cxx17-note 2{{template parameter is declared here}}

using Int = A<E6>;
using Int = A<EE::EE32>; // expected-error {{not implicitly convertible}}
using Int = A<(int)EE::EE32>;
using Int = A<200>;
using Int = A<1000>; // expected-error {{template argument evaluates to 1000, which cannot be narrowed to type 'unsigned char'}}
using Int = A<-3>; // expected-error {{template argument evaluates to -3, which cannot be narrowed to type 'unsigned char'}}

// Note, conversions from integral or unscoped enumeration types to bool are
// integral conversions as well as boolean conversions.
// FIXME: Per core issue 1407, this is not correct.
template<typename T, T v> struct Val { static constexpr T value = v; };
// cxx17-note@-1 2{{template parameter is declared here}}
static_assert(Val<bool, E1>::value == 1, ""); // ok
static_assert(Val<bool, '\0'>::value == 0, ""); // ok
static_assert(Val<bool, U'\1'>::value == 1, ""); // ok
static_assert(Val<bool, E5>::value == 1, ""); // expected-error {{5, which cannot be narrowed to type 'bool'}}

//  function pointer conversions [C++17]
void noexcept_false() noexcept(false);
void noexcept_true() noexcept(true);
Val<decltype(&noexcept_false), &noexcept_true> remove_noexcept;
Val<decltype(&noexcept_true), &noexcept_false> add_noexcept;
#if __cplusplus > 201402L
// expected-error@-2 {{value of type 'void (*)() noexcept(false)' is not implicitly convertible to 'decltype(&noexcept_true)' (aka 'void (*)() noexcept(true)')}}
#endif

// (no other conversions are permitted)
using Int = A<1.0>; // expected-error {{conversion from 'double' to 'unsigned char' is not allowed in a converted constant expression}}
enum B : bool {
  True = &a, // expected-error {{conversion from 'bool (*)(int)' to 'bool' is not allowed in a converted constant expression}}
  False = 0.0, // expected-error {{conversion from 'double' to 'bool' is not allowed in a converted constant expression}}
};
void c() {
  // Note, promoted type of switch is 'int'.
  switch (bool b = a(5)) { // expected-warning {{boolean value}}
  case 0.0f: // expected-error {{conversion from 'float' to 'int' is not allowed in a converted constant expression}}
    break;
  }
}
template <bool B> int f() { return B; } // expected-note {{candidate template ignored: invalid explicitly-specified argument for template parameter 'B'}}
template int f<&S::operator int>(); // expected-error {{does not refer to a function template}}
template int f<(bool)&S::operator int>();

int n = Val<bool, &S::operator int>::value; // expected-error-re {{conversion from 'int (S::*)(){{( __attribute__\(\(thiscall\)\))?}} const' to 'bool' is not allowed in a converted constant expression}}

namespace NonConstLValue {
  struct S {
    constexpr operator int() const { return 10; }
  };
  S s; // not constexpr
  // Under the FDIS, this is not a converted constant expression.
  // Under the new proposed wording, it is.
  enum E : char { e = s };
}