p5.cpp revision d79093af384ac0ea78f4237a001eae7467e06a61
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -fcxx-exceptions %s 2 3namespace StdExample { 4 5constexpr int f(void *) { return 0; } 6constexpr int f(...) { return 1; } 7constexpr int g1() { return f(0); } 8constexpr int g2(int n) { return f(n); } 9constexpr int g3(int n) { return f(n*0); } 10 11namespace N { 12 constexpr int c = 5; 13 constexpr int h() { return c; } 14} 15constexpr int c = 0; 16constexpr int g4() { return N::h(); } 17 18static_assert(f(0) == 0, ""); 19static_assert(f('0') == 1, ""); 20static_assert(g1() == 0, ""); 21static_assert(g2(0) == 1, ""); 22static_assert(g2(1) == 1, ""); 23static_assert(g3(0) == 1, ""); 24static_assert(g3(1) == 1, ""); 25static_assert(N::h() == 5, ""); 26static_assert(g4() == 5, ""); 27 28 29constexpr int f(bool b) 30 { return b ? throw 0 : 0; } // ok 31constexpr int f() { return throw 0, 0; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{subexpression}} 32 33struct B { 34 constexpr B(int x) : i(0) { } 35 int i; 36}; 37 38int global; // expected-note {{declared here}} 39 40struct D : B { 41 constexpr D() : B(global) { } // expected-error {{constexpr constructor never produces a constant expression}} expected-note {{read of non-const}} 42}; 43 44} 45 46namespace PotentialConstant { 47 48constexpr int Comma(int n) { return // expected-error {{constexpr function never produces a constant expression}} 49 (void)(n * 2), 50 throw 0, // expected-note {{subexpression}} 51 0; 52} 53 54int ng; // expected-note 5{{here}} 55constexpr int BinaryOp1(int n) { return n + ng; } // expected-error {{never produces}} expected-note {{read}} 56constexpr int BinaryOp2(int n) { return ng + n; } // expected-error {{never produces}} expected-note {{read}} 57 58double dg; // expected-note 2{{here}} 59constexpr double BinaryOp1(double d) { return d + dg; } // expected-error {{never produces}} expected-note {{read}} 60constexpr double BinaryOp2(double d) { return dg + d; } // expected-error {{never produces}} expected-note {{read}} 61 62constexpr int Add(int a, int b, int c) { return a + b + c; } 63constexpr int FunctionArgs(int a) { return Add(a, ng, a); } // expected-error {{never produces}} expected-note {{read}} 64 65struct S { int a; int b; int c[2]; }; 66constexpr S InitList(int a) { return { a, ng }; }; // expected-error {{never produces}} expected-note {{read}} 67constexpr S InitList2(int a) { return { a, a, { ng } }; }; // expected-error {{never produces}} expected-note {{read}} 68 69constexpr S InitList3(int a) { return a ? (S){ a, a } : (S){ a, ng }; }; // ok 70 71// FIXME: Check both arms of a ?: if the conditional is a potential constant 72// expression with an unknown value, and diagnose if neither is constant. 73constexpr S InitList4(int a) { return a ? (S){ a, ng } : (S){ a, ng }; }; 74 75// __builtin_constant_p ? : is magical, and is always a potential constant. 76constexpr bool BcpCall(int n) { 77 return __builtin_constant_p((int*)n != &n) ? (int*)n != &n : (int*)n != &n; 78} 79static_assert(BcpCall(0), ""); 80 81// DR1311: A function template which can produce a constant expression, but 82// for which a particular specialization cannot, is ok. 83template<typename T> constexpr T cmin(T a, T b) { 84 return a < b ? a : b; 85} 86int n = cmin(3, 5); // ok 87 88struct X { 89 constexpr X() {} 90 bool operator<(X); // not constexpr 91}; 92 93X x = cmin(X(), X()); // ok, not constexpr 94 95// Same with other temploids. 96template<typename T> 97struct Y { 98 constexpr Y() {} 99 constexpr int get() { return T(); } 100}; 101struct Z { operator int(); }; 102 103int y1 = Y<int>().get(); // ok 104int y2 = Y<Z>().get(); // ok 105 106} 107