Home | History | Annotate | Download | only in dcl.constexpr
      1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -verify -std=c++11 -fcxx-exceptions %s
      2 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -std=c++11 -fcxx-exceptions -Wno-invalid-constexpr %s -DNO_INVALID_CONSTEXPR
      3 
      4 namespace StdExample {
      5 
      6 constexpr int f(void *) { return 0; }
      7 constexpr int f(...) { return 1; }
      8 constexpr int g1() { return f(0); }
      9 constexpr int g2(int n) { return f(n); }
     10 constexpr int g3(int n) { return f(n*0); }
     11 
     12 namespace N {
     13   constexpr int c = 5;
     14   constexpr int h() { return c; }
     15 }
     16 constexpr int c = 0;
     17 constexpr int g4() { return N::h(); }
     18 
     19 static_assert(f(0) == 0, "");
     20 static_assert(f('0') == 1, "");
     21 static_assert(g1() == 0, "");
     22 static_assert(g2(0) == 1, "");
     23 static_assert(g2(1) == 1, "");
     24 static_assert(g3(0) == 1, "");
     25 static_assert(g3(1) == 1, "");
     26 static_assert(N::h() == 5, "");
     27 static_assert(g4() == 5, "");
     28 
     29 
     30 constexpr int f(bool b)
     31   { return b ? throw 0 : 0; } // ok
     32 constexpr int f() { return throw 0, 0; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{subexpression}}
     33 
     34 struct B {
     35   constexpr B(int x) : i(0) { }
     36   int i;
     37 };
     38 
     39 int global; // expected-note {{declared here}}
     40 
     41 struct D : B {
     42   constexpr D() : B(global) { } // expected-error {{constexpr constructor never produces a constant expression}} expected-note {{read of non-const}}
     43 };
     44 
     45 }
     46 
     47 namespace PotentialConstant {
     48 
     49 constexpr int Comma(int n) { return // expected-error {{constexpr function never produces a constant expression}}
     50   (void)(n * 2),
     51   throw 0, // expected-note {{subexpression}}
     52   0;
     53 }
     54 
     55 int ng; // expected-note 6{{here}}
     56 constexpr int BinaryOp1(int n) { return n + ng; } // expected-error {{never produces}} expected-note {{read}}
     57 constexpr int BinaryOp2(int n) { return ng + n; } // expected-error {{never produces}} expected-note {{read}}
     58 
     59 double dg; // expected-note 2{{here}}
     60 constexpr double BinaryOp1(double d) { return d + dg; } // expected-error {{never produces}} expected-note {{read}}
     61 constexpr double BinaryOp2(double d) { return dg + d; } // expected-error {{never produces}} expected-note {{read}}
     62 
     63 constexpr int Add(int a, int b, int c) { return a + b + c; }
     64 constexpr int FunctionArgs(int a) { return Add(a, ng, a); } // expected-error {{never produces}} expected-note {{read}}
     65 
     66 struct S { int a; int b; int c[2]; };
     67 constexpr S InitList(int a) { return { a, ng }; }; // expected-error {{never produces}} expected-note {{read}}
     68 constexpr S InitList1a(int a) { return S{ a, ng }; }; // expected-error {{never produces}} expected-note {{read}}
     69 constexpr S InitList2(int a) { return { a, a, { ng } }; }; // expected-error {{never produces}} expected-note {{read}}
     70 constexpr S InitList3(int a) { return a ? S{ a, a } : S{ a, ng }; }; // ok
     71 
     72 constexpr int LogicalAnd1(int n) { return n && (throw, 0); } // ok
     73 constexpr int LogicalAnd2(int n) { return 1 && (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}}
     74 
     75 constexpr int LogicalOr1(int n) { return n || (throw, 0); } // ok
     76 constexpr int LogicalOr2(int n) { return 0 || (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}}
     77 
     78 constexpr int Conditional1(bool b, int n) { return b ? n : ng; } // ok
     79 constexpr int Conditional2(bool b, int n) { return b ? n * ng : n + ng; } // expected-error {{never produces}} expected-note {{both arms of conditional operator are unable to produce a constant expression}}
     80 
     81 // __builtin_constant_p ? : is magical, and is always a potential constant.
     82 constexpr bool BcpCall(int n) {
     83   return __builtin_constant_p((int*)n != &n) ? (int*)n != &n : (int*)n != &n; // expected-warning 3 {{cast to 'int *' from smaller integer type 'int'}}
     84 }
     85 static_assert(BcpCall(0), "");
     86 
     87 // DR1311: A function template which can produce a constant expression, but
     88 // for which a particular specialization cannot, is ok.
     89 template<typename T> constexpr T cmin(T a, T b) {
     90   return a < b ? a : b;
     91 }
     92 int n = cmin(3, 5); // ok
     93 
     94 struct X {
     95   constexpr X() {}
     96   bool operator<(X); // not constexpr
     97 };
     98 
     99 X x = cmin(X(), X()); // ok, not constexpr
    100 
    101 // Same with other temploids.
    102 template<typename T>
    103 struct Y {
    104   constexpr Y() {}
    105   constexpr int get() { return T(); } // expected-warning {{C++1y}}
    106 };
    107 struct Z { operator int(); };
    108 
    109 int y1 = Y<int>().get(); // ok
    110 int y2 = Y<Z>().get(); // ok
    111 
    112 }
    113 
    114 #ifndef NO_INVALID_CONSTEXPR
    115 namespace PR14550 {
    116   // As an "extension", we allow functions which can't produce constant
    117   // expressions to be declared constexpr in system headers (libstdc++
    118   // marks some functions as constexpr which use builtins which we don't
    119   // support constant folding). Ensure that we don't mark those functions
    120   // as invalid after suppressing the diagnostic.
    121 # 122 "p5.cpp" 1 3
    122   int n;
    123   struct A {
    124     static constexpr int f() { return n; }
    125   };
    126   template<typename T> struct B {
    127     B() { g(T::f()); } // expected-error {{undeclared identifier 'g'}}
    128   };
    129 # 130 "p5.cpp" 2
    130   template class B<A>; // expected-note {{here}}
    131 }
    132 #endif
    133