C++N4606 (216) 16.3.5 Scope of macro definitions [cpp.scope] p447

#はじめに
N4606 Working Draft, Standard for Programming Language C++
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/#mailing2016-11
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

n4606は、ISO/IEC JTC1 SC22 WG21の作業原案(Working Draft)です。
公式のISO/IEC 14882原本ではありません。
ISO/IEC JTC1 SC22 WG21では、可能な限り作業文書を公開し、幅広い意見を求めています。
一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(g++, Clang++)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C++, MISRA C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, TOPPERSカーネル、g++(GCC), clang++(LLVM）との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

##作業方針
１）コンパイルエラーを収集する。
２）コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
３）リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
４）意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

１）だけのものから４）まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

## list
N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(1) coding list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/


## Compiler
###clang++ --version
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-apple-darwin17.4.0

###g++-7 --version
g++-7 (Homebrew GCC 7.3.0_1) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.

#(216) 16.3.5 Scope of macro definitions [cpp.scope] p447

```c++:p447.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf
#define msg "(216) 16.3.5 Scope of macro definitions [cpp.scope] p447.cpp"
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>

#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

void func() {
  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(,) };
  char c[2][6] = { str(hello), str() };

//result in
//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);
//int i[] = { 1, 23, 4, 5, };
//char c[2][6] = { "hello", "" };
}

#define str(s) # s
#define xstr(s) str(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)

func2() {
  debug(1, 2);
  fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away
            == 0) str(: @\n), s);

  glue(HIGH, LOW);
  xglue(HIGH, LOW);

//result in
//printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
//fputs("strncmp(\"abc\\0d\", \"abc\", ’\\4’) == 0" ": @\n", s);

}
//#include "vers2.h" (after macro replacement, before file access)

//"hello";
//"hello" ", world"

//or, after concatenation of the character string literals,

//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"

#define t(x,y,z) x ## y ## z
void func3() {
  int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
              t(10,,), t(,11,), t(,,12), t(,,)
            };
}
//results in
//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
*/ )
//But the following redefinitions are invalid:
#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__))

void func4() {
  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);
//results in
//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));
}

int main() {
  std::cout<< msg << std::endl;
  return EXIT_SUCCESS;
}
```

```shell_session:cppgl17.sh
$ ./cppgl17.sh p447
$ clang++ p447.cpp -std=c++17 
p447.cpp:29:3: error: use of undeclared identifier 'y'
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
  ^
p447.cpp:29:14: error: use of undeclared identifier 'z'
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
             ^
p447.cpp:18:11: note: expanded from macro 'z'
#define z z[0]
          ^
p447.cpp:29:24: error: use of undeclared identifier 'f'
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
                       ^
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:29:32: error: use of undeclared identifier 't'
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
                               ^
p447.cpp:30:6: warning: expression result unused [-Wunused-value]
g(x+(3,4)-w) | h 5) & m
     ^
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:14:21: note: expanded from macro 'f'
#define f(a) f(x * (a))
                    ^
p447.cpp:30:10: warning: expression result unused [-Wunused-value]
g(x+(3,4)-w) | h 5) & m
~~~~~~~~~^~~
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:14:21: note: expanded from macro 'f'
#define f(a) f(x * (a))
                    ^
p447.cpp:30:1: error: use of undeclared identifier 'f'
g(x+(3,4)-w) | h 5) & m
^
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:14:14: note: expanded from macro 'f'
#define f(a) f(x * (a))
             ^
p447.cpp:30:16: error: use of undeclared identifier 'f'
g(x+(3,4)-w) | h 5) & m
               ^
p447.cpp:19:11: note: expanded from macro 'h'
#define h g(~
          ^
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:14:14: note: expanded from macro 'f'
#define f(a) f(x * (a))
             ^
p447.cpp:30:23: warning: expression result unused [-Wunused-value]
g(x+(3,4)-w) | h 5) & m
                      ^
p447.cpp:20:16: note: expanded from macro 'm'
#define m(a) a(w)
               ^
p447.cpp:21:11: note: expanded from macro 'w'
#define w 0,1
          ^
p447.cpp:14:21: note: expanded from macro 'f'
#define f(a) f(x * (a))
                    ^
p447.cpp:31:2: error: use of undeclared identifier 'f'
(f)^m(m);
 ^
p447.cpp:31:7: error: use of undeclared identifier 'm'
(f)^m(m);
      ^
p447.cpp:42:9: warning: 'str' macro redefined [-Wmacro-redefined]
#define str(s) # s
        ^
p447.cpp:26:9: note: previous definition is here
#define str(x) # x
        ^
p447.cpp:52:26: error: unterminated function-like macro invocation
#include xstr(INCFILE(2).h)
                         ^
p447.cpp:19:11: note: expanded from macro 'h'
#define h g(~
          ^
p447.cpp:17:11: note: expanded from macro 'g'
#define g f
          ^
p447.cpp:14:9: note: macro 'f' defined here
#define f(a) f(x * (a))
        ^
p447.cpp:52:10: fatal error: 'vers2.' file not found
#include xstr(INCFILE(2).h)
         ^~~~~~~~~~~~~~~~~
p447.cpp:43:17: note: expanded from macro 'xstr'
#define xstr(s) str(s)
                ^~~~~
p447.cpp:42:16: note: expanded from macro 'str'
#define str(s) # s
               ^~
<scratch space>:307:1: note: expanded from here
"vers2."
^~~~~~~~
4 warnings and 10 errors generated.

$ g++-7 p447.cpp -std=c++17 
p447.cpp:42:0: warning: "str" redefined
 #define str(s) # s
 
p447.cpp:26:0: note: this is the location of the previous definition
 #define str(x) # x
 
p447.cpp:52:1: error: unterminated argument list invoking macro "f"
 #include xstr(INCFILE(2).h)
 ^~~~~
p447.cpp:81:0: warning: "t" redefined
 #define t(x,y,z) x ## y ## z
 
p447.cpp:22:0: note: this is the location of the previous definition
 #define t(a) a
 
p447.cpp:97:0: warning: "OBJ_LIKE" redefined
 #define OBJ_LIKE (0) // different token sequence
 
p447.cpp:91:0: note: this is the location of the previous definition
 #define OBJ_LIKE /* white space */ (1-1) /* other */
 
p447.cpp:98:0: warning: "OBJ_LIKE" redefined
 #define OBJ_LIKE (1 - 1) // different white space
 
p447.cpp:97:0: note: this is the location of the previous definition
 #define OBJ_LIKE (0) // different token sequence
 
p447.cpp:99:0: warning: "FUNC_LIKE" redefined
 #define FUNC_LIKE(b) ( a ) // different parameter usage
 
p447.cpp:93:0: note: this is the location of the previous definition
 #define FUNC_LIKE( a )( /* note the white space */ \
 
p447.cpp:100:0: warning: "FUNC_LIKE" redefined
 #define FUNC_LIKE(b) ( b ) // different parameter spelling
 
p447.cpp:99:0: note: this is the location of the previous definition
 #define FUNC_LIKE(b) ( a ) // different parameter usage
 
p447.cpp:102:0: warning: "debug" redefined
 #define debug(...) fprintf(stderr, __VA_ARGS__)
 
p447.cpp:44:0: note: this is the location of the previous definition
 #define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
 
p447.cpp: In function 'void func()':
p447.cpp:29:3: error: 'y' was not declared in this scope
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
   ^
p447.cpp:14:21: note: in definition of macro 'f'
 #define f(a) f(x * (a))
                     ^
p447.cpp:14:14: error: 'f' was not declared in this scope
 #define f(a) f(x * (a))
              ^
p447.cpp:29:1: note: in expansion of macro 'f'
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
 ^
p447.cpp:18:11: error: 'z' was not declared in this scope
 #define z z[0]
           ^
p447.cpp:14:21: note: in definition of macro 'f'
 #define f(a) f(x * (a))
                     ^
p447.cpp:29:12: note: in expansion of macro 'f'
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
            ^
p447.cpp:29:14: note: in expansion of macro 'z'
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
              ^
p447.cpp:29:32: error: 't' was not declared in this scope
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
                                ^
p447.cpp:22:14: note: in definition of macro 't'
 #define t(a) a
              ^
p447.cpp:29:32: note: suggested alternative: 'tm'
 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
                                ^
p447.cpp:22:14: note: in definition of macro 't'
 #define t(a) a
              ^
p447.cpp:31:7: error: 'm' was not declared in this scope
 (f)^m(m);
       ^
p447.cpp:20:14: note: in definition of macro 'm'
 #define m(a) a(w)
              ^
p447.cpp:31:7: note: suggested alternative: 'tm'
 (f)^m(m);
       ^
p447.cpp:20:14: note: in definition of macro 'm'
 #define m(a) a(w)
              ^
p447.cpp: At global scope:
p447.cpp:54:7: error: ISO C++ forbids declaration of 'func2' with no type [-fpermissive]
 func2(){
       ^
p447.cpp: In function 'int func2()':
p447.cpp:45:1: error: 'x1' was not declared in this scope
 x ## s, x ## t)
 ^
p447.cpp:55:1: note: in expansion of macro 'debug'
 debug(1, 2);
 ^~~~~
p447.cpp:45:1: note: suggested alternative: 'x'
 x ## s, x ## t)
 ^
p447.cpp:55:1: note: in expansion of macro 'debug'
 debug(1, 2);
 ^~~~~
p447.cpp:45:9: error: 'x2' was not declared in this scope
 x ## s, x ## t)
         ^
p447.cpp:55:1: note: in expansion of macro 'debug'
 debug(1, 2);
 ^~~~~
p447.cpp:45:9: note: suggested alternative: 'x'
 x ## s, x ## t)
         ^
p447.cpp:55:1: note: in expansion of macro 'debug'
 debug(1, 2);
 ^~~~~
p447.cpp:57:19: error: 's' was not declared in this scope
 == 0) str(: @\n), s);
                   ^
p447.cpp: In function 'void func4()':
p447.cpp:110:10: error: 'y' was not declared in this scope
 report(x>y, "x is %d but y is %d", x, y);
          ^
p447.cpp:104:29: note: in definition of macro 'report'
 #define report(test, ...) ((test) ? puts(#test) : printf(__VA_ARGS__))
                             ^~~~
```

タブを２つの空白に変換しているスクリプトは下記。

```sh:a.sh
#!/bin/bash
astyle -s2 -c < $1.cpp > $1s2.cpp
cat $1s2.cpp
```

#検討事項
コンパイルエラーをなくす方法
役に立つまたは意味のある出力


#参考資料


N4606 Working Draft 2016, ISO/IEC 14882, C++ standardのコード断片をコンパイルするためにしていること
https://qiita.com/kaizen_nagoya/items/a8d7ee2f2e29e76c19c1


コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)
https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da

Clang/Clang++(LLVM) gcc/g++(GNU) コンパイラ警告等比較
https://qiita.com/kaizen_nagoya/items/9a82b958cc3aeef0403f

Qiitaに投稿するCのStyle例(暫定)
https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

MISRA C++ 5-0-16
https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

C++ Templates Part1 BASICS Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp
https://qiita.com/kaizen_nagoya/items/cd5fc49106fad5a4e9ed


ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)
https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1

C言語（C++)に対する誤解、曲解、無理解、爽快。
https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識
https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9

'wchar.h' file not found で困った clang++ macOS
https://qiita.com/kaizen_nagoya/items/de15cd46d657517fac11

Open POSIX Test Suiteの使い方を調べはじめました
https://qiita.com/kaizen_nagoya/items/644d5e407f5faf96e6dc

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認
https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

どうやって MISRA Example Suiteをコンパイルするか
https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C まとめ #include <misra_c.h>
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

「C++完全理解ガイド」の同意できること上位10
https://qiita.com/kaizen_nagoya/items/aa5744e0c4a8618c7671

#文書履歴
0.10 初稿 20180422
0.11 result in関連を//註釈に 20180422