// 1 filename:cpp2011-16-3-5.cpp // ver 0.1 June.12, 2014 // // 2 original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf // >16 Preprocessing directives 16.3 Macro replacement 16.3.5 Scope of macro definitions // // 3 compile and output mechanism: // (c) Dr. OGAWA Kiyoshi, kaizen at gifu-u.ac.jp, // // 4 compile errors and/or warnings: // 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) // Target: x86_64-apple-darwin13.2.0, Thread model: posix // Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-16-3-5.cpp // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. // This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // http://gcc.gnu.org/onlinedocs/gcc/Standards.html // Command/Options: g++-4.9 -std=c++11 -Wall cpp2011-16-3-5.cpp // g++-4.9: error: unrecognized command line option '-stdlib=libc++' // Configuration:brew install gcc49 // // 4.3. Visual Studio Express 2013, // (c) Microsoft http://www.visualstudio.com/ // SPEC: // Windows 7, .NET Framework // (c) VMware, Inc. // VMWare fusion 6 // // 5. Hardware: MacBook Pro, //(c) Intel http://ark.intel.com/products/37006/ //Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3 // // 6. Special Thanks: Upper organizatios and // ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html // Renesas Electronics Corporation.http://www.renesas.com/ // NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/ // Toyo Corporation, http://www.toyo.co.jp/English/ // Japan Standard Association, http://bit.ly/1lzykg1 // NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html // Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html // WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html // SevenWise.co., http://www.7ws.co.jp/index.html // TOYOTA Motor Corporation, http://toyota.jp/ // IT planning Inc., http://www.itpl.co.jp/en/index.html // DENSO Corporation, http://www.globaldenso.com/en/ // Aisin Seiki co. Ltd., http://www.aisin.com/ // Spancion Inc., http://www.spansion.com/ // Yazaki Corporation, http://www.yazaki-group.com/global/ // Pananosic Corporation, http://www.panasonic.net/ // SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp // CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/ // JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/ // OSC:Open Source Conference, http://www.ospn.jp/ #include //#include //#include //#include //#include //#include //#include //#include //#include using namespace std; #define TABSIZE 100 int table[TABSIZE]; // #define max(a, b) ((a) > (b) ? (a) : (b)) // #define x 3 #define f(a) f(x * (a)) #undef x #define x 2 #define g f #define z z[0] #define h g(~ #define m(a) a(w) #define w 0,1 #define t(a) a #define p() int #define q(x) x #define r(x,y) x ## y #define str(x) # x char c[2][6] = { str(hello), str() }; // int i[] = { 1, 23, 4, 5, }; char c2[2][6] = { "hello", "" }; // #define str2(s) # s #define xstr(s) str2(s) #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \ x ## s, x ## t) #define INCFILE(n) vers ## n #define glue(a, b) a ## b #define xglue(a, b) glue(a, b) #define HIGHLOW "hello" #define LOW LOW ", world" #include xstr(INCFILE(2).h) #include "vers2.h" (after macro replacement, before file access) // #define t(x,y,z) x ## y ## z int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), t(10,,), t(,11,), t(,,12), t(,,) }; // int j[] = { 123, 45, 67, 89, 10, 11, 12, }; // #define OBJ_LIKE (1-1) #define OBJ_LIKE /* white space */ (1-1) /* other */ #define FUNC_LIKE(a) ( a ) #define FUNC_LIKE( a )( /* note the white space */ \ a /* other stuff on this line */ ) // #define OBJ_LIKE (0) // different token sequence #define OBJ_LIKE (1 - 1) // different white space #define FUNC_LIKE(b) ( a ) // different parameter usage #define FUNC_LIKE(b) ( b ) // different parameter spelling // #define debug(...) fprintf(stderr, _ _ VA_ARGS _ _) #define showlist(...) puts(#_ _ VA_ARGS _ _) #define report(test, ...) ((test) ? puts(#test) : printf(_ _ VA_ARGS _ _)) int main() { f(y+1) + f(f(z)) % t(t(g)(0) + t)(1); g(x+(3,4)-w) | h 5) & m (f)^m(m); p() i[q()] = { q(1), r(2,3), r(4,), r(,5), r(,) }; // f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1); f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1); // "hello"; "hello" ", world" // debug(1, 2); fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away == 0) str2(: @\n), s); // glue(HIGH, LOW); xglue(HIGH, LOW) // printf("x" "1" "= %d, x" "2" "= %s", x1, x2); fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s); // printf("x1= %d, x2= %s", x1, x2); fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0: @\n", s); #include "vers2.h" (after macro replacement, before file access) "hello"; "hello, world" // debug("Flag"); debug("X = %d\n", x); showlist(The first, second, and third items.); report(x>y, "x is %d but y is %d", x, y); // fprintf(stderr, "Flag"); fprintf(stderr, "X = %d\n", x); puts("The first, second, and third items."); ((x>y) ? puts("x>y") : printf("x is %d but y is %d", x, y)); // cout << "16 Preprocessing directives 16.3 Macro replacement 16.3.5 Scope of macro definitions"<:176:1: note: expanded from here "vers2." ^ 2 errors generated.