142 lines · cpp
1// Integer literals2const char Ch1 = 'a';3const signed char Ch2 = 'b';4const unsigned char Ch3 = 'c';5 6const wchar_t Ch4 = L'd';7const signed wchar_t Ch5 = L'e';8const unsigned wchar_t Ch6 = L'f';9 10const short C1 = 12;11const unsigned short C2 = 13;12 13const int C3 = 12;14const unsigned int C4 = 13;15 16const long C5 = 22;17const unsigned long C6 = 23;18 19const long long C7 = 66;20const unsigned long long C8 = 67;21 22 23// String literals24const char str1[] = "ABCD";25const char str2[] = "ABCD" "0123";26 27const wchar_t wstr1[] = L"DEF";28const wchar_t wstr2[] = L"DEF" L"123";29 30 31// Boolean literals32const bool bval1 = true;33const bool bval2 = false;34 35// Floating Literals36const float F1 = 12.2F;37const double F2 = 1E4;38const long double F3 = 1.2E-3L;39 40 41// nullptr literal42const void *vptr = nullptr;43 44 45int glb_1[4] = { 10, 20, 30, 40 };46 47struct S1 {48 int a;49 int b[3];50};51 52struct S2 {53 int c;54 S1 d;55};56 57S2 glb_2 = { 22, .d.a = 44, .d.b[0] = 55, .d.b[1] = 66 };58 59void testNewThrowDelete() {60 throw;61 char *p = new char[10];62 delete[] p;63}64 65int testArrayElement(int *x, int n) {66 return x[n];67}68 69int testTernaryOp(int c, int x, int y) {70 return c ? x : y;71}72 73S1 &testConstCast(const S1 &x) {74 return const_cast<S1&>(x);75}76 77S1 &testStaticCast(S1 &x) {78 return static_cast<S1&>(x);79}80 81S1 &testReinterpretCast(S1 &x) {82 return reinterpret_cast<S1&>(x);83}84 85S1 &testDynamicCast(S1 &x) {86 return dynamic_cast<S1&>(x);87}88 89int testScalarInit(int x) {90 return int(x);91}92 93struct S {94 float f;95 double d;96};97struct T {98 int i;99 struct S s[10];100};101 102void testOffsetOf() {103 __builtin_offsetof(struct T, s[2].d);104}105 106 107int testDefaultArg(int a = 2*2) {108 return a;109}110 111int testDefaultArgExpr() {112 return testDefaultArg();113}114 115template <typename T> // T has TemplateTypeParmType116void testTemplateTypeParmType(int i);117 118void useTemplateType() {119 testTemplateTypeParmType<char>(4);120}121 122const bool ExpressionTrait = __is_lvalue_expr(1);123const unsigned ArrayRank = __array_rank(int[10][20]);124const unsigned ArrayExtent = __array_extent(int[10][20], 1);125 126constexpr int testLambdaAdd(int toAdd) {127 const int Captured1 = 1, Captured2 = 2;128 constexpr auto LambdaAdd = [Captured1, Captured2](int k) -> int {129 return Captured1 + Captured2 + k;130 };131 return LambdaAdd(toAdd);132}133 134template<typename T>135struct TestLambdaTemplate {136 T i, j;137 TestLambdaTemplate(T i, const T &j) : i(i), j(j) {}138 T testLambda(T k) {139 return [this](T k) -> decltype(auto) { return i + j + k; }(k);140 }141};142