// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++98 -verify -fsyntax-only %s -Wno-c++11-extensions -Wno-c++1y-extensions -DPRECXX11 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++1y -verify -fsyntax-only %s -DCPP1Y #define CONST const #ifdef PRECXX11 #define static_assert _Static_assert #endif class A { template CONST T wrong; // expected-error {{member 'wrong' declared as a template}} template CONST T wrong_init = 5; // expected-error {{member 'wrong_init' declared as a template}} template static CONST T right = T(100); template static CONST T right = 5; template CONST int right; // expected-error {{member 'right' declared as a template}} template CONST float right = 5; // expected-error {{member 'right' declared as a template}} template<> static CONST int right = 7; template<> static CONST float right; template static CONST int right; // expected-error {{expected '<' after 'template'}} }; namespace out_of_line { class B0 { template static CONST T right = T(100); template static CONST T right = T(5); }; template<> CONST int B0::right = 7; // expected-note {{previous}} template CONST int B0::right; // expected-warning {{has no effect}} template<> CONST int B0::right; // expected-note {{previous}} template CONST int B0::right; // expected-warning {{has no effect}} class B1 { template static CONST T right; template static CONST T right; }; template CONST T B1::right = T(100); template CONST T B1::right = T(5); class B2 { template static CONST T right = T(100); // expected-note {{previous initialization is here}} template static CONST T right = T(5); // expected-note {{previous initialization is here}} }; template CONST T B2::right = T(100); // expected-error {{static data member 'right' already has an initializer}} template CONST T B2::right = T(5); // expected-error {{static data member 'right' already has an initializer}} class B3 { template static CONST T right = T(100); template static CONST T right = T(5); }; template CONST T B3::right; template CONST T B3::right; class B4 { template static CONST T a; template static CONST T a = T(100); template static CONST T b = T(100); template static CONST T b; }; template CONST T B4::a; // expected-error {{default initialization of an object of const type 'const int'}} template CONST T B4::a; template CONST int B4::a; // expected-note {{in instantiation of}} template CONST int B4::a; template CONST T B4::b; template CONST T B4::b; // expected-error {{default initialization of an object of const type 'const int'}} template CONST int B4::b; template CONST int B4::b; // expected-note {{in instantiation of}} } namespace non_const_init { class A { template static T wrong_inst_undefined = T(10); // expected-note {{refers here}} template static T wrong_inst_defined = T(10); // expected-error {{non-const static data member must be initialized out of line}} template static T wrong_inst_out_of_line; }; template const int A::wrong_inst_undefined; // expected-error {{undefined}} template T A::wrong_inst_defined; template const int A::wrong_inst_defined; template int A::wrong_inst_defined; // expected-note {{in instantiation of static data member 'non_const_init::A::wrong_inst_defined' requested here}} template T A::wrong_inst_out_of_line = T(10); template int A::wrong_inst_out_of_line; class B { template static T wrong_inst; // expected-note {{refers here}} template static T wrong_inst = T(100); // expected-error {{non-const static data member must be initialized out of line}} expected-note {{refers here}} template static T wrong_inst_fixed; template static T wrong_inst_fixed; }; template int B::wrong_inst; // expected-error {{undefined}} // FIXME: It'd be better to produce the 'explicit instantiation of undefined // template' diagnostic here, not the 'must be initialized out of line' // diagnostic. template int B::wrong_inst; // expected-note {{in instantiation of static data member 'non_const_init::B::wrong_inst' requested here}} template const int B::wrong_inst; // expected-error {{undefined}} template T B::wrong_inst_fixed = T(100); template int B::wrong_inst_fixed; class C { template static CONST T right_inst = T(10); // expected-note {{here}} template static CONST T right_inst = T(100); // expected-note {{here}} }; template CONST int C::right_inst; // expected-error {{undefined variable template}} template CONST int C::right_inst; // expected-error {{undefined variable template}} namespace pointers { struct C0 { template static U Data; template static CONST U Data = U(); // expected-note {{here}} template static U Data2; template static CONST U Data2 = U(); }; const int c0_test = C0::Data; static_assert(c0_test == 0, ""); template const int C0::Data; // expected-error {{undefined}} template const U C0::Data2; template const int C0::Data2; struct C1a { template static U Data; template static U* Data; // Okay, with out-of-line definition }; template T* C1a::Data = new T(); template int* C1a::Data; struct C1b { template static U Data; template static CONST U* Data; // Okay, with out-of-line definition }; template CONST T* C1b::Data = (T*)(0); template CONST int* C1b::Data; struct C2a { template static int Data; template static U* Data = new U(); // expected-error {{non-const static data member must be initialized out of line}} }; template int* C2a::Data; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2a::Data' requested here}} struct C2b { template static int Data; template static U *const Data = (U*)(0); // expected-error {{static data member of type 'int *const'}} }; template U *const C2b::Data; template int *const C2b::Data; // expected-note {{in instantiation of static data member 'non_const_init::pointers::C2b::Data' requested here}} } } #ifndef PRECXX11 namespace constexpred { class A { template constexpr T wrong; // expected-error {{member 'wrong' declared as a template}} \ // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}} template constexpr T wrong_init = 5; // expected-error {{non-static data member cannot be constexpr; did you intend to make it static?}} template static constexpr T right = T(100); template static constexpr T right = 5; template constexpr int right; // expected-error {{member 'right' declared as a template}} \ // expected-error {{non-static data member cannot be constexpr; did you intend to make it const?}} template constexpr float right = 5; // expected-error {{non-static data member cannot be constexpr; did you intend to make it static?}} template<> static constexpr int right = 7; template <> static constexpr float right; // expected-error {{declaration of constexpr static data member 'right' requires an initializer}} template static constexpr int right; // expected-error {{expected '<' after 'template'}} }; } #endif namespace in_class_template { template class D0 { template static U Data; // expected-note {{here}} template static CONST U Data = U(); }; template CONST int D0::Data; template int D0::Data; // expected-error {{undefined}} template template const U D0::Data; template class D1 { template static U Data; template static U* Data; }; template template U* D1::Data = (U*)(0); template int* D1::Data; // expected-note {{previous}} template int* D1::Data; // expected-error {{duplicate explicit instantiation}} template class D2 { template static U Data; template static U* Data; }; template<> template U* D2::Data = (U*)(0) + 1; template int* D2::Data; // expected-note {{previous}} template int* D2::Data; // expected-error {{duplicate explicit instantiation}} template struct D3 { template static CONST U Data = U(100); // expected-note {{here}} }; static_assert(D3::Data == 100, ""); template const char D3::Data; // expected-error {{undefined}} namespace bug_files { template class D0a { template static U Data; template static CONST U Data = U(10); // expected-note {{previous declaration is here}} }; template<> template U D0a::Data = U(100); // expected-error {{redefinition of 'Data'}} // FIXME: We should accept this, and the corresponding case for class // templates. // // [temp.class.spec.mfunc]/2: If the primary member template is explicitly // specialized for a given specialization of the enclosing class template, // the partial specializations of the member template are ignored template class D1 { template static U Data; template static CONST U Data = U(10); // expected-note {{previous declaration is here}} }; template<> template U D1::Data = U(10); template<> template U D1::Data = U(100); // expected-error{{redefinition of 'Data'}} } namespace definition_after_outer_instantiation { template struct S { template static const int V1; template static const int V2; // expected-note 3{{here}} }; template struct S; template template const int S::V1 = 123; template template const int S::V2 = 456; static_assert(S::V1 == 123, ""); // FIXME: The first and third case below possibly should be accepted. We're // not picking up partial specializations added after the primary template // is instantiated. This is kind of implied by [temp.class.spec.mfunc]/2, // and matches our behavior for member class templates, but it's not clear // that this is intentional. See PR17294 and core-24030. static_assert(S::V2 == 456, ""); // FIXME expected-error {{}} expected-note {{initializer of 'V2' is unknown}} static_assert(S::V2 == 789, ""); // expected-error {{}} expected-note {{initializer of 'V2' is unknown}} template template const int S::V2 = 789; static_assert(S::V2 == 789, ""); // FIXME expected-error {{}} expected-note {{initializer of 'V2' is unknown}} // All is OK if the partial specialization is declared before the implicit // instantiation of the class template specialization. static_assert(S::V1 == 123, ""); static_assert(S::V2 == 456, ""); static_assert(S::V2 == 789, ""); } namespace incomplete_array { template extern T var[]; template T var[] = { 1, 2, 3 }; template<> char var[] = "hello"; template char var[] = "pointer"; static_assert(sizeof(var) == 12, ""); static_assert(sizeof(var) == 6, ""); static_assert(sizeof(var) == 8, ""); template struct tuple; template struct A { template static T x[]; template static T y[]; template static T y >[]; }; int *use_before_definition = A::x; template template T A::x[sizeof(U)]; static_assert(sizeof(A::x) == 4, ""); template template T A::y >[] = { U()... }; static_assert(sizeof(A::y >) == 12, ""); } namespace bad_reference { struct S { template static int A; // expected-note 4{{here}} }; template void f() { typename T::template A a; // expected-error {{template name refers to non-type template 'S::template A'}} } template void g() { T::template A::B = 0; // expected-error {{template name refers to non-type template 'S::template A'}} } template void h() { class T::template A c; // expected-error {{template name refers to non-type template 'S::template A'}} } template struct X : T::template A {}; // expected-error {{template name refers to non-type template 'S::template A'}} template void f(); // expected-note {{in instantiation of}} template void g(); // expected-note {{in instantiation of}} template void h(); // expected-note {{in instantiation of}} template struct X; // expected-note {{in instantiation of}} } } namespace in_nested_classes { // TODO: } namespace bitfield { struct S { template static int f : I; // expected-error {{static member 'f' cannot be a bit-field}} }; } namespace b20896909 { // This used to crash. template struct helper {}; template class A { template static helper x; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} }; void test() { A ai; // expected-note {{in instantiation of}} } } namespace member_access_is_ok { #ifdef CPP1Y namespace ns1 { struct A { template constexpr static T Var = N; }; static_assert(A{}.Var == 5,""); } // end ns1 #endif // CPP1Y namespace ns2 { template struct A { template static T&& Var; }; template template T&& A::Var = T(N + M); int *AV = &A().Var; } //end ns2 } // end ns member_access_is_ok #ifdef CPP1Y namespace PR24473 { struct Value { template static constexpr T value = 0; }; template struct Something { void foo() { static_assert(TValue::template value == 0, ""); // error } }; int main() { Something{}.foo(); return 0; } } // end ns PR24473 #endif // CPP1Y namespace dependent_static_var_template { struct A { template static int n; // expected-note 2{{here}} }; int &r = A::template n; // expected-error {{use of variable template 'n' requires template arguments}} template int &f() { return T::template n; } // expected-error {{use of variable template 'n' requires template arguments}} int &s = f(); // expected-note {{instantiation of}} namespace B { template static int n; // expected-note {{here}} } int &t = B::template n; // expected-error {{use of variable template 'n' requires template arguments}} } #ifndef PRECXX11 namespace template_vars_in_template { template struct TakesInt {}; template struct S { template static constexpr int v = 42; template void mf() { constexpr int val = v; } void mf2() { constexpr int val = v; TakesInt ti; (void)ti.x; // expected-error{{no member named 'x' in 'template_vars_in_template::TakesInt<42>'}} } }; void useit() { S x; x.mf(); x.mf2(); // expected-note{{in instantiation of member function 'template_vars_in_template::S::mf2' requested here}} } } #endif