| [f47ba55] | 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
 | 
|---|
| [db98cd5] | 12 | // Last Modified On : Sun Apr 29 16:51:42 2018
 | 
|---|
 | 13 | // Update Count     : 54
 | 
|---|
| [f47ba55] | 14 | // 
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include <fstream>
 | 
|---|
| [eb68ebb] | 17 | #include <wchar.h>
 | 
|---|
 | 18 | #include <uchar.h>
 | 
|---|
 | 19 | 
 | 
|---|
| [cd218e8] | 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; }
 | 
|---|
| [eb68ebb] | 25 | 
 | 
|---|
| [cd218e8] | 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; }
 | 
|---|
| [eb68ebb] | 31 | 
 | 
|---|
| [f47ba55] | 32 | 
 | 
|---|
| [ea46db7] | 33 | struct Weight { double stones; };
 | 
|---|
 | 34 | void ?{}( Weight & w ) { w.stones = 0; }
 | 
|---|
| [f47ba55] | 35 | void ?{}( Weight & w, double w ) { w.stones = w; }
 | 
|---|
| [ea46db7] | 36 | Weight ?+?( Weight l, Weight r ) {
 | 
|---|
 | 37 |         return (Weight){ l.stones + r.stones };
 | 
|---|
 | 38 | }
 | 
|---|
| [09687aa] | 39 | ofstream & ?|?( ofstream & os, Weight w ) { return os | w.stones; }
 | 
|---|
| [f47ba55] | 40 | 
 | 
|---|
| [cd218e8] | 41 | Weight ?`st( double w ) { return (Weight){ w }; }               // backquote for user literals
 | 
|---|
| [f47ba55] | 42 | Weight ?`lb( double w ) { return (Weight){ w / 14.0 }; }
 | 
|---|
| [ea46db7] | 43 | Weight ?`kg( double w ) { return (Weight) { w * 0.16 }; }
 | 
|---|
| [eb68ebb] | 44 | 
 | 
|---|
| [f47ba55] | 45 | int main() {
 | 
|---|
| [ea46db7] | 46 |         Weight w, heavy = { 20 };                                                       // 20 stone
 | 
|---|
| [cd218e8] | 47 |         w = 155`lb;
 | 
|---|
 | 48 |         sout | w | endl;
 | 
|---|
| [ea46db7] | 49 |         w = 0b_1111`st;
 | 
|---|
| [cd218e8] | 50 |         sout | w | endl;
 | 
|---|
 | 51 |         w = 0_233`lb;                                                                           // octal weight (155)
 | 
|---|
 | 52 |         sout | w | endl;
 | 
|---|
| [ea46db7] | 53 |         w = 0x_9b_u`kg;
 | 
|---|
 | 54 |         sout | w | endl;
 | 
|---|
 | 55 |         w = 70.3`kg;
 | 
|---|
 | 56 |         sout | w | endl;
 | 
|---|
 | 57 |         w = 11`st + 1`lb;
 | 
|---|
 | 58 |         sout | w | endl;
 | 
|---|
 | 59 |         w = 5`st + 8`kg + 25`lb + heavy;
 | 
|---|
| [cd218e8] | 60 |         sout | w | endl;
 | 
|---|
| [f47ba55] | 61 | 
 | 
|---|
| [eb68ebb] | 62 | //      0`secs;
 | 
|---|
 | 63 |         1`s;
 | 
|---|
 | 64 |         23`s;
 | 
|---|
 | 65 |         23u`m;
 | 
|---|
 | 66 |         23l`h;
 | 
|---|
 | 67 |         23_ul`_A_;
 | 
|---|
 | 68 |         1_234_LL`__thingy_;
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |         0xff_ffl;
 | 
|---|
 | 71 |         0xff_ff`s;
 | 
|---|
 | 72 |         0xff_ffu`m;
 | 
|---|
 | 73 |         0xff_ffl`h;
 | 
|---|
 | 74 |         0xff_fful`_A_;
 | 
|---|
 | 75 |         0xff_ffLL`__thingy_;
 | 
|---|
 | 76 | 
 | 
|---|
| [cd218e8] | 77 |         '\n'`s;
 | 
|---|
 | 78 |         L'\n'`h;
 | 
|---|
 | 79 |         u'\n'`m;
 | 
|---|
 | 80 |         L_'\n'`_A_;
 | 
|---|
 | 81 |         U_'\n'`__thingy_;
 | 
|---|
| [f47ba55] | 82 | 
 | 
|---|
| [eb68ebb] | 83 |         "abc"`s;
 | 
|---|
| [db98cd5] | 84 | //      u"abc"`m;
 | 
|---|
| [b6dc097] | 85 | //      U_"abc"`h;
 | 
|---|
 | 86 | //      L"abc"`_A_;
 | 
|---|
| [eb68ebb] | 87 |         u8_"abc"`__thingy_;
 | 
|---|
 | 88 | } // main
 | 
|---|
| [f47ba55] | 89 | 
 | 
|---|
 | 90 | // Local Variables: //
 | 
|---|
 | 91 | // tab-width: 4 //
 | 
|---|
 | 92 | // compile-command: "cfa user_literals.c" //
 | 
|---|
 | 93 | // End: //
 | 
|---|