1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // user_literals.c --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Wed Sep 6 21:40:50 2017
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Sat Sep 9 08:31:32 2017
|
---|
13 | // Update Count : 48
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <fstream>
|
---|
17 | #include <wchar.h>
|
---|
18 | #include <uchar.h>
|
---|
19 |
|
---|
20 | int ?`s( int s ) { sout | "secs" | s | endl; return s; }
|
---|
21 | int ?`m( int m ) { sout | "mins" | m | endl; return m * 60; }
|
---|
22 | int ?`h( int h ) { sout | "hours" | h | endl; return h * 3600; }
|
---|
23 | int ?`_A_( int x ) { sout | "_A_" | x | endl; return x; }
|
---|
24 | int ?`__thingy_( int x ) { sout | "_thingy_" | x | endl; return x; }
|
---|
25 |
|
---|
26 | int ?`s( const char * s ) { sout | "secs" | s | endl; return 0; }
|
---|
27 | int ?`m( const char16_t * m ) { sout | "mins" | m | endl; return 0;}
|
---|
28 | int ?`h( const char32_t * h ) { sout | "hours" | h | endl; return 0; }
|
---|
29 | int ?`_A_( const wchar_t * str ) { sout | "_A_" | str | endl; return 0; }
|
---|
30 | int ?`__thingy_( const char * str ) { sout | "_thingy_" | str | endl; return 0; }
|
---|
31 |
|
---|
32 |
|
---|
33 | struct Weight {
|
---|
34 | double stones;
|
---|
35 | };
|
---|
36 | void ?{}( Weight & w ) { w.stones = 0; } // operations
|
---|
37 | void ?{}( Weight & w, double w ) { w.stones = w; }
|
---|
38 | Weight ?+?( Weight l, Weight r ) { return (Weight){ l.stones + r.stones }; }
|
---|
39 | ofstream * ?|?( ofstream * os, Weight w ) { return os | w.stones; }
|
---|
40 |
|
---|
41 | Weight ?`st( double w ) { return (Weight){ w }; } // backquote for user literals
|
---|
42 | Weight ?`lb( double w ) { return (Weight){ w / 14.0 }; }
|
---|
43 | Weight ?`kg( double w ) { return (Weight) { w * 0.1575}; }
|
---|
44 |
|
---|
45 |
|
---|
46 | int main() {
|
---|
47 | Weight w, hw = { 14 }; // 14 stone
|
---|
48 | w = 11`st + 1`lb;
|
---|
49 | sout | w | endl;
|
---|
50 | w = 70.3`kg;
|
---|
51 | sout | w | endl;
|
---|
52 | w = 155`lb;
|
---|
53 | sout | w | endl;
|
---|
54 | w = 0x_9b_u`lb; // hexadecimal unsigned weight (155)
|
---|
55 | sout | w | endl;
|
---|
56 | w = 0_233`lb; // octal weight (155)
|
---|
57 | sout | w | endl;
|
---|
58 | w = 5`st + 8`kg + 25`lb + hw;
|
---|
59 | sout | w | endl;
|
---|
60 |
|
---|
61 | // 0`secs;
|
---|
62 | 1`s;
|
---|
63 | 23`s;
|
---|
64 | 23u`m;
|
---|
65 | 23l`h;
|
---|
66 | 23_ul`_A_;
|
---|
67 | 1_234_LL`__thingy_;
|
---|
68 |
|
---|
69 | 0xff_ffl;
|
---|
70 | 0xff_ff`s;
|
---|
71 | 0xff_ffu`m;
|
---|
72 | 0xff_ffl`h;
|
---|
73 | 0xff_fful`_A_;
|
---|
74 | 0xff_ffLL`__thingy_;
|
---|
75 |
|
---|
76 | '\n'`s;
|
---|
77 | L'\n'`h;
|
---|
78 | u'\n'`m;
|
---|
79 | L_'\n'`_A_;
|
---|
80 | U_'\n'`__thingy_;
|
---|
81 |
|
---|
82 | "abc"`s;
|
---|
83 | u"abc"`m;
|
---|
84 | U_"abc"`h;
|
---|
85 | L"abc"`_A_;
|
---|
86 | u8_"abc"`__thingy_;
|
---|
87 | } // main
|
---|
88 |
|
---|
89 | // Local Variables: //
|
---|
90 | // tab-width: 4 //
|
---|
91 | // compile-command: "cfa user_literals.c" //
|
---|
92 | // End: //
|
---|