forked from jeff-hykin/better-cpp-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_examples.cpp
More file actions
378 lines (337 loc) · 8.85 KB
/
syntax_examples.cpp
File metadata and controls
378 lines (337 loc) · 8.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <iostream>
#include <sstream>
//
//
// Constants
//
//
//
// Digits
//
// decimal
1
.1
.239480
.239480f
0.
0.f
0.L
0.LL
4897430la
32094.930123a
4897430LL
32094.930123F
32094.930123f
1'03'432'43
123232'1231321
3'20'94.93'01'23
3'20'94.93'1'23
// e
0e1
1e10f
1.e10
1.e10
1.e-10
1.79769e+308
1.79'76'9e+3'0'8
// octal
01
01001202
010'0'120'2
0'1'2'3'4
// binary
0b101010
0b000001
0b100001
0b1'01'010
0b1'00'001
// hex
0x01
0xabcdef
0xaBCDEf
0xABCDEF
0xAB.cdp5f
0xAB.cdp5l
0x20394afLL
0x01
0xabc'def
0xa'BC'DEf
0xABC'DEF
0x20'394'a'fLL
// hex floating point literal
0x0.5p10F
0x0.5p10f
0x1ffp10
0x0.23p10
0x0.234985p10L
0x139804.234985p10L
0x0.53'84'92p10
0x5.p10
0x0.23p+10
0x0.234985p+10L
0x139804.234985p-10L
0x0.53'84'92p-10
0x5.p+10
0x13'98'04.234985p10L
// custom literals
29042ms
0xabcdefmm
0xabc'defmm
0xabcdefyards
20ounces
2000miles
L"akdjfhald"
//
// chars
//
'1'
'a'
'\n'
'\0'
//
// Strings
//
auto a = "things\n\b\v\t";
//
// type castings
//
dynamic_cast <int> (expression + 1 + thing);
reinterpret_cast <double> (expression);
static_cast <Custom> (expression);
const_cast <int> (expression);
{
dynamic_cast <int> (expression + 1 + thing);
reinterpret_cast <double> (expression);
static_cast <Custom> (expression);
const_cast <int> (expression);
}
//
// Storage types
//
pthread_rwlockattr_t thing;
padfthread_rwlockattr_t thing;
decltype(int);
//
// operators
//
typeid()
sizeof()
alignas()
//
// Memory
//
auto a = new int(5);
delete a;
new (&a_storage_of_callable.callable) type(forward<Callable>(a_callable));
int *array = new int[100];
delete[] array;
char should_always_be_a_newline;
char deleter;
//
//
// namespaces
//
//
using namespace std;
using namespace std;using namespace std;
using namespace parent_namespace::std;
inline namespace {};
namespace {};
namespace scoped::console { };
namespace console {
template <typename ANYTYPE> void __MAGIC__show(ANYTYPE input) {
// by default use the stream operator with cout
std::cout << input;
}
}
//
// Scope resolution
//
std::cout << input;
numeric_limits<long double>::infinity();
char_traits<ANYTYPE>::eof();
numeric_limits<streamsize>::max();
Task<ANY_OUTPUT_TYPE, ANY_INPUT_TYPE>::links_to;
&TEST_CLASS::name;
Event<ANYTYPE>:: ListenersFor[input_event.name]
//
// member access
//
window.as.translate(0,0.5,0);
window.MV.translate(0,0.5,0);
a_pointer.thread;
a_pointer.*thread;
a_pointer->thread;
a_pointer->*thread;
a_pointer.thread.thing;
a_pointer.*thread.*thing;
a_pointer->thread->thing;
a_pointer->*thread->*thing;
a_pointer.thread->*thing;
a_pointer.*thread->thing;
a_pointer->thread.*thing;
a_pointer->*thread.thing;
a_pointer.thread[0];
a_pointer.*thread[0];
a_pointer->thread[0];
a_pointer->*thread[0];
a_pointer.thread[0]->*thing;
a_pointer.*thread[0]->thing;
a_pointer->thread[0].*thing;
a_pointer->*thread[0].thing;
a_pointer.thread();
a_pointer.*thread();
ptr_to_original->Start();
ptr_to_original->*Start();
a_pointer.thread()->*thing;
a_pointer.*thread()->thing;
ptr_to_original->Start().*thing;
ptr_to_original->*Start().thing;
{
a_pointer.thread[0]->*thing;
a_pointer.*thread[0]->thing;
a_pointer->thread[0].*thing;
a_pointer->*thread[0].thing;
a_pointer.thread;
a_pointer.*thread;
a_pointer->thread;
a_pointer->*thread;
}
{
a_pointer.thread();
a_pointer.*thread();
ptr_to_original->Start();
ptr_to_original->*Start();
}
//
// Operator keyword
//
ostream& operator<<(ostream& out, const Item& input_) {};
Item operator+( const string& base , const int& repetitions ) {};
Item operator-( const int& the_input , const Item& input_item ) {};
Item operator/( const Item& input_item , const int& the_input ) {};
Item operator^( const Item& input_item , const int& the_input ) {};
// implicit conversions
operator std::string() const {};
operator double() const {};
// custom literal
void operator "" _km(long double);
//
//
// preprocessor
//
//
#define Infinite numeric_limits<long double>::infinity()
#define DoubleMax 1.79769e+308
#define Pi 3.1415926535897932384626
#define show(argument) cout << #argument << "\n";
#ifndef CEKO_LIBRARY
#define CEKO_LIBRARY
#endif
//
// templates
//
namespace test {
template <class T>
struct test {
// seperate line template
template <class U = std::vector<int>>
template<typename RETURN_TYPE, int N = 0 > 1, typename Callable>
bool operator()(U k) {}
};
struct test2 {
// same-line template
template <class U = std::vector<int>> bool operator()(U k) {}
};
struct test3 {
// same-line template
template <class U = std::vector<int>> bool operator()(U k) {}
};
struct test2 {
bool operator()() = delete;
};
} // namespace test
// no syntax highlighting
class test2 {};
template<int, typename Callable, typename Ret, typename... Args>
auto internalConversionToFuncPtr(Callable&& a_callable, Ret (*)(Args...))
{
static bool used = false;
static storage<Callable> a_storage_of_callable;
using type = decltype(a_storage_of_callable.callable);
if(used) {
a_storage_of_callable.callable.~type();
}
new (&a_storage_of_callable.callable) type(forward<Callable>(a_callable));
used = true;
// lambda
return [](Args... args) -> Ret {
return Ret(a_storage_of_callable.callable(forward<Args>(args)...));
};
}
template<typename RETURN_TYPE, int N = 0, typename Callable>
RETURN_TYPE* convertToFunctionPointer(Callable&& c)
{
return internalConversionToFuncPtr<N>(forward<Callable>(c), (RETURN_TYPE*)nullptr);
}
//
// Classes
//
class Thing {
public:
public :
private :
private:
protected:
auto a = 1;
Thing() {
}
};
struct Thing2 {
public:
public :
private :
private:
protected:
auto a = 1;
Thing() {
}
};
//
// Functions
//
template <class ANYTYPE>
string ToBinary(ANYTYPE input)
{
// depends on #include <bitset>
return bitset<8>(input).to_string();
}
//
// lambdas
//
auto a = [ a, b, c ]
(Args... args, int thing1) -> Ret
{
}
[ a, b, c ] (Args... args, int thing1) mutable -> Ret { }
[ a, b, c ] (Args... args, int thing1) { }
[ a = stuff::blah[1324], b, c ] (Args... args, int thing1) { }
[ a, b, c ] { }
[=] -> int { }
[=] () mutable { return; }
auto a = thing[1][2];
auto a = thing()[2];
[](Args... args) -> Ret {
return Ret(a_storage_of_callable.callable(forward<Args>(args)...));
};
return [ a, b, c ] (Args... args, int thing1) -> Ret { }
return [ a, b, c ] -> int { }
//
// not lambdas
//
test()[0] = 5; // no syntax highlighting;
test[5][5] = 5;
int main() {
int a = ( thing + 10)
return 0;
}