// 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, ""); } namespace Unevaluated { // We follow g++ in treating any reference to a constexpr function template // specialization as requiring an instantiation, even if it occurs in an // unevaluated context. // // We go slightly further than g++, and also trigger the implicit definition // of a defaulted special member in the same circumstances. This seems scary, // since a lot of classes have constexpr special members in C++11, but the // only observable impact should be the implicit instantiation of constexpr // special member templates (defaulted special members should only be // generated if they are well-formed, and non-constexpr special members in a // base or member cause the class's special member to not be constexpr). // // FIXME: None of this is required by the C++ standard. The rules in this // area are poorly specified, so this is subject to change. namespace NotConstexpr { template struct S { S() : n(0) {} S(const S&) : n(T::error) {} int n; }; struct U : S {}; decltype(U(U())) u; // ok, don't instantiate S::S() because it wasn't declared constexpr } namespace Constexpr { template struct S { constexpr S() : n(0) {} constexpr S(const S&) : n(T::error) {} // expected-error {{has no members}} int n; }; struct U : S {}; // expected-note {{instantiation}} decltype(U(U())) u; // expected-note {{here}} } namespace PR11851_Comment0 { template constexpr int f() { return x; } template void ovf(int (&x)[f()]); void f() { int x[10]; ovf<10>(x); } } namespace PR11851_Comment1 { template constexpr bool Integral() { return true; } template()> struct safe_make_unsigned { typedef T type; }; template using Make_unsigned = typename safe_make_unsigned::type; template struct get_distance_type { using type = int; }; template auto size(R) -> Make_unsigned::type>; auto check() -> decltype(size(0)); } namespace PR11851_Comment6 { template struct foo {}; template constexpr int bar() { return 0; } template foo()> foobar(); auto foobar_ = foobar(); } namespace PR11851_Comment9 { struct S1 { constexpr S1() {} constexpr operator int() const { return 0; } }; int k1 = sizeof(short{S1(S1())}); struct S2 { constexpr S2() {} constexpr operator int() const { return 123456; } }; int k2 = sizeof(short{S2(S2())}); // expected-error {{cannot be narrowed}} expected-note {{override}} } namespace PR12288 { template constexpr bool foo() { return true; } template struct bar {}; template bar()> baz() { return bar()>(); } int main() { baz(); } } namespace PR13423 { template struct enable_if {}; template struct enable_if { using type = T; }; template struct F { template static constexpr bool f() { return sizeof(T) < U::size; } template static typename enable_if(), void>::type g() {} // expected-note {{disabled by 'enable_if'}} }; struct U { static constexpr int size = 2; }; void h() { F::g(); } void i() { F::g(); } // expected-error {{no matching function}} } namespace PR14203 { struct duration { constexpr duration() {} }; template void sleep_for() { constexpr duration max = duration(); } } } namespace NoInstantiationWhenSelectingOverload { // Check that we don't instantiate conversion functions when we're checking // for the existence of an implicit conversion sequence, only when a function // is actually chosen by overload resolution. struct S { template constexpr S(T) : n(T::error) {} // expected-error {{no members}} int n; }; int f(S); int f(int); void g() { f(0); } void h() { (void)sizeof(f(0)); } void i() { (void)sizeof(f("oops")); } // expected-note {{instantiation of}} }