From 222e2a7620e6520ffaf4fc4e69d79c18da31542e Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Mon, 24 Sep 2012 09:58:17 +1000 Subject: Add the clang library to the repo (with some of my changes, too). --- clang/test/SemaTemplate/constexpr-instantiate.cpp | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 clang/test/SemaTemplate/constexpr-instantiate.cpp (limited to 'clang/test/SemaTemplate/constexpr-instantiate.cpp') diff --git a/clang/test/SemaTemplate/constexpr-instantiate.cpp b/clang/test/SemaTemplate/constexpr-instantiate.cpp new file mode 100644 index 0000000..2f9fe0e --- /dev/null +++ b/clang/test/SemaTemplate/constexpr-instantiate.cpp @@ -0,0 +1,77 @@ +// RUN: %clang_cc1 -std=c++11 -verify %s + +namespace UseBeforeDefinition { + struct A { + template static constexpr T get() { return T(); } + // ok, not a constant expression. + int n = get(); + }; + + // ok, constant expression. + constexpr int j = A::get(); + + template constexpr int consume(T); + // ok, not a constant expression. + const int k = consume(0); // expected-note {{here}} + + template constexpr int consume(T) { return 0; } + // ok, constant expression. + constexpr int l = consume(0); + + constexpr int m = k; // expected-error {{constant expression}} expected-note {{initializer of 'k'}} +} + +namespace IntegralConst { + template constexpr T f(T n) { return n; } + enum E { + v = f(0), w = f(1) // ok + }; + static_assert(w == 1, ""); + + char arr[f('x')]; // ok + static_assert(sizeof(arr) == 'x', ""); +} + +namespace ConvertedConst { + template constexpr T f(T n) { return n; } + int f() { + switch (f()) { + case f(4): return 0; + } + return 1; + } +} + +namespace OverloadResolution { + template constexpr T f(T t) { return t; } + + template struct S { }; + + template auto g(T t) -> S &; + char &f(...); + + template auto h(T t[f(sizeof(T))]) -> decltype(&*t) { + return t; + } + + S<4> &k = g(0); + int *p, *q = h(p); +} + +namespace DataMember { + template struct S { static const int k; }; + const int n = S::k; // expected-note {{here}} + template const int S::k = 0; + constexpr int m = S::k; // ok + constexpr int o = n; // expected-error {{constant expression}} expected-note {{initializer of 'n'}} +} + +namespace Reference { + const int k = 5; + template struct S { + static volatile int &r; + }; + template volatile int &S::r = const_cast(k); + constexpr int n = const_cast(S::r); + static_assert(n == 5, ""); +} -- cgit v1.2.3