HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Oreo
|
8.0.0_r4
下载
查看原文件
收藏
根目录
external
clang
test
SemaCXX
cxx1y-variable-templates_in_class.cpp
// RUN: %clang_cc1 -std=c++98 -verify -fsyntax-only %s -Wno-c++11-extensions -Wno-c++1y-extensions -DPRECXX11 // RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s -DCPP1Y #define CONST const #ifdef PRECXX11 #define static_assert(expr, msg) typedef int static_assert[(expr) ? 1 : -1]; #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; // expected-error {{explicit specialization of 'right' in class scope}} template<> static CONST float right
; // expected-error {{explicit specialization of 'right' in class scope}} template static CONST int right
; // expected-error {{template specialization requires 'template<>'}} \ // expected-error {{explicit specialization of 'right' in class scope}} }; 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; template CONST int B0::right
; template<> CONST int B0::right
; template CONST int B0::right
; 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; // expected-error {{explicit specialization of 'right' in class scope}} template<> static constexpr float right
; // expected-error {{explicit specialization of 'right' in class scope}} template static constexpr int right
; // expected-error {{template specialization requires 'template<>'}} \ // expected-error {{explicit specialization of 'right' in class scope}} }; } #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; }; 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 {{}} static_assert(S
::V2
== 789, ""); // expected-error {{}} template
template
const int S
::V2
= 789; static_assert(S
::V2
== 789, ""); // FIXME expected-error {{}} // 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::A'}} } template
void g() { T::template A
::B = 0; // expected-error {{template name refers to non-type template 'S::A'}} } template
void h() { class T::template A
c; // expected-error {{template name refers to non-type template 'S::A'}} } template
struct X : T::template A
{}; // expected-error {{template name refers to non-type template 'S::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