Skip to content

Commit 1ce4c11

Browse files
committed
decltype test
1 parent b88c550 commit 1ce4c11

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

understanding-c++11/decltype.cpp

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,75 @@ TEST( Decltype, FourRulesForUsingDecltype ) {
6464
int && rvalueReference();
6565
const bool func( int );
6666

67+
bool same( false );
68+
69+
//------------------------------------------------------
6770
//1. e is id-expression(without brackets) or member visit operator. --> decltype(e) == T.
68-
decltype(arr) v1;
69-
decltype(ptr) v2;
70-
decltype(s.si) v3;
71+
decltype(arr) v1; // int[10]
72+
same = std::is_same<decltype(v1), int[10]>::value;
73+
EXPECT_TRUE( same );
74+
75+
decltype(ptr) v2; // int*
76+
same = std::is_same<decltype(v2), int*>::value;
77+
EXPECT_TRUE( same );
78+
79+
decltype(s.si) v3; // int
80+
same = std::is_same<decltype(v3), int>::value;
81+
EXPECT_TRUE( same );
82+
7183
//decltype(foo) v4; // error: foo is overloaded.
84+
//------------------------------------------------------
7285

86+
//------------------------------------------------------
7387
// 2. e == xvalue --> decltype(e) == rvalue-reference.
74-
decltype(rvalueReference) v5;
75-
88+
decltype(rvalueReference) v5; // int&&
89+
same = std::is_same<decltype(v5), int&&>::value;
90+
EXPECT_TRUE( same );
91+
//------------------------------------------------------
92+
93+
//------------------------------------------------------
7694
// 3. e --> lvalue --> decltype(e)==lvalue-reference.
7795
// following var must be bound with i, because they are all l-value-reference.
78-
decltype(true ? i : i) v6 = i;
79-
decltype((i)) var7 = i;
80-
decltype(++i) var8 = i;
81-
decltype(arr[0]) var9 = i;
82-
decltype("hello") var10 = "12345"; // const char (&var10) [5];
83-
decltype(*ptr) var11 = i;
96+
decltype(true ? i : i) v6 = i; // int&
97+
same = std::is_same<decltype(v6), int&>::value;
98+
EXPECT_TRUE( same );
99+
100+
decltype((i)) v7 = i; // int&
101+
same = std::is_same<decltype(v7), int&>::value;
102+
EXPECT_TRUE( same );
103+
104+
decltype(++i) v8 = i; // int&
105+
same = std::is_same<decltype(v8), int&>::value;
106+
EXPECT_TRUE( same );
84107

108+
decltype(arr[0]) v9 = i; // int&, operator[] return l-value
109+
same = std::is_same<decltype(v9), int&>::value;
110+
EXPECT_TRUE( same );
111+
112+
decltype("hello") v10 = "12345"; // const char (&v10) [5];
113+
same = std::is_same<decltype(v10), const char (&)[5]>::value;
114+
EXPECT_TRUE( same );
115+
116+
decltype(*ptr) v11 = i; // int&, operator* return l-value
117+
same = std::is_same<decltype(v11), int&>::value;
118+
EXPECT_TRUE( same );
119+
//------------------------------------------------------
120+
121+
122+
//------------------------------------------------------
85123
// 4. other situations, e --> decltype(e) == e.
86-
decltype(1) var12;
87-
decltype(i++) var13;
88-
decltype(func( 1 )) var14 = 1000;
124+
decltype(1) v12; // int
125+
same = std::is_same<decltype(v12), int>::value;
126+
EXPECT_TRUE( same );
127+
128+
decltype(i++) v13; // int
129+
same = std::is_same<decltype(v13), int>::value;
130+
EXPECT_TRUE( same );
131+
132+
decltype(func( 1 )) v14 = 1000; // const bool
133+
same = std::is_same<decltype(v14), const bool>::value;
134+
EXPECT_TRUE( same );
135+
//------------------------------------------------------
89136
}
90137

91138

0 commit comments

Comments
 (0)