C++N4606(275) Annex C (informative) Compatibility [diff]p1419

#はじめに(Introduction)
C++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

C++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）との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

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

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

## C++N4606符号断片編纂一覧(example code compile list)
C++N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile 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.

#(275) Annex C (informative) Compatibility [diff]p1419
##算譜(source code)

```c++:p1419.cpp
// C++N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf
#define msg "C++N460(275) Annex C (informative) Compatibility [diff]p1419.cpp"
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
#include <cstdlib>

// C.1 C++ and ISO C[diff.iso]
// C.1.1 Clause 2: lexical conventions [diff.lex]
int function( int i ) {
  return i;
};
int function( char c ) {
  return (int)c;
};
void c11() {
  function( 'x' );
  if (sizeof('x') == sizeof(int) )  std::cout<<"sizeof('x') == sizeof(int)"<<std::endl;
#ifdef _C_
  char* p = "abc"; // valid in C, invalid in C++
#endif
}
void f(char*) {
  char* p = (char*)"abc"; // OK: cast added
  f(p);
  f((char*)"def"); // OK: cast added
}
//C.1.2 Clause 3: basic concepts [diff.basic]
void c12() {
  int i;
#ifdef _C_
  int i;///valid c, invalid c++
#endif
  struct X {
    int i;
    struct X* next;
  };
  static struct X a;
  static struct X b = { 0, &a };
#ifdef _C_
  static struct X a = { 1, &b };
#endif
}
//C.1.3 Clause 4: standard conversions [diff.conv]
char a[10];
void* b=a;
void foo() {
#ifdef _C_
  char* c=b;
#endif
}
char* c = (char*) b;
//C.1.4 Clause 5: expressions [diff.expr]
#ifdef _C_
void * p = (void*)(struct x {int i;} *)0;
#endif
void c14() {
  char arr[100];
  std::cout <<"sizeof(0, arr)="<< sizeof(0, arr) <<std::endl;
}
//C.1.5 Clause 6: statements [diff.stat
//C.1.6 Clause 7: declarations [diff.dcl]
#ifdef _C_
static struct S { // valid C, invalid in C++
  int i;
};
#endif

#ifdef _C_
typedef struct name1 { /*...*/
} name1; // valid C and C++
#endif

struct name { /*...*/
};

#ifdef _C_
typedef int name2; // valid C, invalid C++
#endif

class name2 { /*...*/
};
name2 i; // i has type class name2

#ifdef _C_
void f2(const parm); //C
const n = 3;//C
//main() //C

#else
void f2(const int parm);//C++
//int main()//C++
const int n = 3;//C++
#endif

/* ... */ /* ... */
#ifdef _C_
void f3() {
  auto int x; // valid C, invalid C++
}
#endif

#ifdef _C_
enum color { red, blue, green };
enum color c = 1; // valid C, invalid C++
#endif

enum e { A };

void c16() {
#ifdef _C_
  if (sizeof(A) == sizeof(int)) ; // in C
#else
  if(sizeof(A) == sizeof(e)) std::cout <<"sizeof(A) == sizeof(e)) " << std::endl; // in C++
#endif
  /* and sizeof(int) is not necessarily equal to sizeof(e) */
}
int f4(); // means int f4(void) in C++
// int f4( unknown ) in C

#ifdef _C_
void f( struct S { int a; } arg ) {} // valid C, invalid C++
enum E { A, B, C } f() {} // valid C, invalid C++
#endif

#ifdef _C_
char array[4] = "abcd"; // valid C, invalid C++
#endif
//C.1.8 Clause 9: classes [diff.class]
int x[99];
void f5() {
  struct x {
    int a;
  };
  sizeof(x); /* size of the array in C */
  /* size of the struct in C++ */
}

struct X {
  struct Y { /* ... */
  } y;
};
#ifdef _C_
struct Y yy; // valid C, invalid C++
#endif

struct Y2; // struct Y2 and struct X2 are at the same scope
struct X2 {
  struct Y2 { /* ... */
  } y2;
};

typedef int I;
struct S {
  I i;
#ifdef _C_
  int I; // valid C, invalid C++
#endif
};
//C.1.9 Clause 12: special member functions [diff.special]
void c19() {
  struct X {
    int i;
  };
  volatile struct X x1 = {0};

#ifdef _C_
  struct X x2(x1); // invalid C++
  struct X x3;
  x3 = x1; // also invalid C++
#endif
}
//C.1.10 Clause 16: preprocessing directives [diff.cpp]

int main()
{
  c11();
  c12();
  c14();
  c16();
  c19();
  foo();
  f(msg);
  std::cout<< msg << std::endl;
  return EXIT_SUCCESS;
}
```
##編纂・実行結果(compile and go)


```shell_session:cppall.sh
$ ./cppall.sh  p1419c
$ clang++ p1419c.cpp -std=c++03 -Wall
p1419c.cpp:21:15: warning: all paths through this function will call itself
      [-Winfinite-recursion]
void f(char*) {
              ^
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
  int i;
      ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
  static struct X b = { 0, &a };
                  ^
p1419c.cpp:133:3: warning: expression result unused [-Wunused-value]
  sizeof(x); /* size of the array in C */
  ^~~~~~~~~
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
  volatile struct X x1 = {0};
                    ^
p1419c.cpp:181:5: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  f(msg);
    ^
p1419c.cpp:3:13: note: expanded from macro 'msg'
#define msg "C++N460(275) Annex C (informative) Compatibility [diff]p1419.cpp"
            ^
p1419c.cpp:91:11: warning: unused variable 'n' [-Wunused-const-variable]
const int n = 3;//C++
          ^
7 warnings generated.
sizeof(0, arr)=100
sizeof(A) == sizeof(e)) 
./cppall.sh: line 6: 56868 Segmentation fault: 11  ./$1l03 $2
$ clang++ p1419c.cpp -std=c++11 -Wall
p1419c.cpp:21:15: warning: all paths through this function will call itself
      [-Winfinite-recursion]
void f(char*) {
              ^
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
  int i;
      ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
  static struct X b = { 0, &a };
                  ^
p1419c.cpp:133:3: warning: expression result unused [-Wunused-value]
  sizeof(x); /* size of the array in C */
  ^~~~~~~~~
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
  volatile struct X x1 = {0};
                    ^
p1419c.cpp:181:5: warning: ISO C++11 does not allow conversion from string literal to
      'char *' [-Wwritable-strings]
  f(msg);
    ^
p1419c.cpp:3:13: note: expanded from macro 'msg'
#define msg "C++N460(275) Annex C (informative) Compatibility [diff]p1419.cpp"
            ^
p1419c.cpp:91:11: warning: unused variable 'n' [-Wunused-const-variable]
const int n = 3;//C++
          ^
7 warnings generated.
sizeof(0, arr)=100
sizeof(A) == sizeof(e)) 
./cppall.sh: line 11: 56872 Segmentation fault: 11  ./$1l11 $2
$ clang++ p1419c.cpp -std=c++17 -Wall
p1419c.cpp:21:15: warning: all paths through this function will call itself
      [-Winfinite-recursion]
void f(char*) {
              ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
  static struct X b = { 0, &a };
                  ^
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
  int i;
      ^
p1419c.cpp:133:3: warning: expression result unused [-Wunused-value]
  sizeof(x); /* size of the array in C */
  ^~~~~~~~~
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
  volatile struct X x1 = {0};
                    ^
p1419c.cpp:181:5: warning: ISO C++11 does not allow conversion from string literal to
      'char *' [-Wwritable-strings]
  f(msg);
    ^
p1419c.cpp:3:13: note: expanded from macro 'msg'
#define msg "C++N460(275) Annex C (informative) Compatibility [diff]p1419.cpp"
            ^
p1419c.cpp:91:11: warning: unused variable 'n' [-Wunused-const-variable]
const int n = 3;//C++
          ^
7 warnings generated.
sizeof(0, arr)=100
sizeof(A) == sizeof(e)) 
./cppall.sh: line 16: 56876 Segmentation fault: 11  ./$1l17 $2

$ g++-7 p1419c.cpp -std=c++03  -Wall
In file included from /usr/local/Cellar/gcc/7.3.0_1/include/c++/7.3.0/future:35:0,
                 from p1419c.cpp:8:
/usr/local/Cellar/gcc/7.3.0_1/include/c++/7.3.0/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^~~~~
p1419c.cpp: In function 'void c12()':
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
   int i;
       ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^
p1419c.cpp: In function 'void c14()':
p1419c.cpp:57:45: warning: left operand of comma operator has no effect [-Wunused-valu]
   std::cout <<"sizeof(0, arr)="<< sizeof(0, arr) <<std::endl;
                                             ^~~
p1419c.cpp: In function 'void f5()':
p1419c.cpp:133:9: warning: statement has no effect [-Wunused-value]
   sizeof(x); /* size of the array in C */
         ^
p1419c.cpp: In function 'void c19()':
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
   volatile struct X x1 = {0};
                     ^~
p1419c.cpp: In function 'int main()':
p1419c.cpp:181:8: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
   f(msg);
        ^
p1419c.cpp: At global scope:
p1419c.cpp:37:19: warning: 'b' defined but not used [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^

$ g++-7 p1419c.cpp -std=c++11  -Wall
p1419c.cpp: In function 'void c12()':
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
   int i;
       ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^
p1419c.cpp: In function 'void c14()':
p1419c.cpp:57:45: warning: left operand of comma operator has no effect [-Wunused-valu]
   std::cout <<"sizeof(0, arr)="<< sizeof(0, arr) <<std::endl;
                                             ^~~
p1419c.cpp: In function 'void f5()':
p1419c.cpp:133:9: warning: statement has no effect [-Wunused-value]
   sizeof(x); /* size of the array in C */
         ^
p1419c.cpp: In function 'void c19()':
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
   volatile struct X x1 = {0};
                     ^~
p1419c.cpp: In function 'int main()':
p1419c.cpp:181:8: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   f(msg);
        ^
p1419c.cpp: At global scope:
p1419c.cpp:37:19: warning: 'b' defined but not used [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^
sizeof(0, arr)=100
sizeof(A) == sizeof(e)) 
./cppall.sh: line 28: 56888 Segmentation fault: 11  ./$1g11 $2

$ g++-7 p1419c.cpp -std=c++17  -Wall
p1419c.cpp: In function 'void c12()':
p1419c.cpp:28:7: warning: unused variable 'i' [-Wunused-variable]
   int i;
       ^
p1419c.cpp:37:19: warning: unused variable 'b' [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^
p1419c.cpp: In function 'void c14()':
p1419c.cpp:57:45: warning: left operand of comma operator has no effect [-Wunused-valu]
   std::cout <<"sizeof(0, arr)="<< sizeof(0, arr) <<std::endl;
                                             ^~~
p1419c.cpp: In function 'void f5()':
p1419c.cpp:133:9: warning: statement has no effect [-Wunused-value]
   sizeof(x); /* size of the array in C */
         ^
p1419c.cpp: In function 'void c19()':
p1419c.cpp:163:21: warning: unused variable 'x1' [-Wunused-variable]
   volatile struct X x1 = {0};
                     ^~
p1419c.cpp: In function 'int main()':
p1419c.cpp:181:8: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   f(msg);
        ^
p1419c.cpp: At global scope:
p1419c.cpp:37:19: warning: 'b' defined but not used [-Wunused-variable]
   static struct X b = { 0, &a };
                   ^
sizeof(0, arr)=100
sizeof(A) == sizeof(e)) 
./cppall.sh: line 34: 56897 Segmentation fault: 11  ./$1g17 $2
```

#検討事項(agenda)
実行時エラーを取る修正
役に立つまたは意味のある出力

#参考資料(reference)

docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7

C++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

C++2003とC++2017でコンパイルエラーになるならない事例集
https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff

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

cpprefjpのdecltypeをコンパイル試験
https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67

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

#文書履歴(document history)
ver. 0.10 初稿 20180502