LoginSignup
0
0

More than 1 year has passed since last update.

6.7.1 Memory model [intro.memory] C++N4910:2022 (21) p59.cpp

Last updated at Posted at 2022-06-14

はじめに

N4910 Working Draft, Standard for Programming Language C++
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf

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

作業方針

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

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

C++N4910:2022

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

C++N4741:2018

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list
https://qiita.com/kaizen_nagoya/items/3294c014044550896010

C++N4606:2016

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

C++N3242 2011 sample code compile list(一覧)

C++2011が出て、コンパイラが対応し始めたころ、N3242のコード断片をコンパイルしながら、C++2011の内容と、コンパイラの対応状況の調査を始めました。
Templateの断片のコンパイル方法がうまくなく、コンパイルエラーの儘になっています。今回、C++N4606を-std=C++03, -std=C++11, -std=C++17でコンパイルする中で意味のある出力を出す方法を検討しています。

C++N3242,2011 2014/06/11 -std=c++11

Xcode 5.0.2/LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)
g++-4.9 (GCC) 4.9.0 20131229 (experimental)
https://researchmap.jp/jooracidk-1797580/#_1797580

Compiler

clang++ --version

Debian clang version 14.0.5-++20220610033153+c12386ae247c-1~exp1~20220610153237.151
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

g++ -version

g++ (GCC) 12.1.0
Copyright (C) 2022 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.

dockerにclang

C++N4606(2). 1.7 The C++ memory model, bit-field[intro.memory]p6

6.7.1 Memory model [intro.memory] C++N4910:2022 (21) p59.cpp

p59.cpp
// N910 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf p.59
const char * msg="6.7.1 Memory model [intro.memory] C++N4910:2022 (21) p59.cpp";
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.


#include <iostream>
#include <cstdlib>

/// from here p.6 
   struct {
     char a;
     int b:5,
     c:11,
     :0,
     d:8;
     struct {int ee:8;} e;
} st; /// add st;
/// to here p.6

int main(int argc, char *argv[], char *envp[]){///
  st.a='C';///
  st.b=747;///
  st.c=123456789;///
  st.d=747;///
  st.e.ee=747;///
  std::cout << "a=" <<st.a<<" b="<<st.b<<" c="<<st.c<<" d="<<st.d<<" e.ee="<<st.e.ee<< std::endl;///
  std::cout << msg << std::endl;///
  return EXIT_SUCCESS;///

編纂・実行結果(compile and go)

bash
# clang++ p59.cpp -std=C++21
error: invalid value 'C++21' in '-std=C++21'
note: use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard
note: use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard
note: use 'c++11' for 'ISO C++ 2011 with amendments' standard
note: use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard
note: use 'c++14' for 'ISO C++ 2014 with amendments' standard
note: use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard
note: use 'c++17' for 'ISO C++ 2017 with amendments' standard
note: use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard
note: use 'c++20' for 'ISO C++ 2020 DIS' standard
note: use 'gnu++20' for 'ISO C++ 2020 DIS with GNU extensions' standard
note: use 'c++2b' for 'Working draft for ISO C++ 2023 DIS' standard
note: use 'gnu++2b' for 'Working draft for ISO C++ 2023 DIS with GNU extensions' standard
# clang++ p59.cpp -std=c++2b
p59.cpp:21:7: warning: implicit truncation from 'int' to bit-field changes value from 747 to 11 [-Wbitfield-constant-conversion]
  st.b=747;///
      ^~~~
p59.cpp:22:7: warning: implicit truncation from 'int' to bit-field changes value from 123456789 to -747 [-Wbitfield-constant-conversion]
  st.c=123456789;///
      ^~~~~~~~~~
p59.cpp:23:7: warning: implicit truncation from 'int' to bit-field changes value from 747 to -21 [-Wbitfield-constant-conversion]
  st.d=747;///
      ^~~~
p59.cpp:24:10: warning: implicit truncation from 'int' to bit-field changes value from 747 to -21 [-Wbitfield-constant-conversion]
  st.e.ee=747;///
         ^~~~
4 warnings generated.
#  ./a.out
a=C b=11 c=-747 d=-21 e.ee=-21
C++N4910. 6..17 Memory model [intro.memo

# g++ p59.cpp
p59.cpp: In function 'int main(int, char**, char**)':
p59.cpp:21:8: warning: overflow in conversion from 'int' to 'signed char:5' changes value from '747' to '11' [-Woverflow]
   21 |   st.b=747;///
      |        ^~~
p59.cpp:22:8: warning: overflow in conversion from 'int' to 'short int:11' changes value from '123456789' to '-747' [-Woverflow]
   22 |   st.c=123456789;///
      |        ^~~~~~~~~
p59.cpp:23:8: warning: overflow in conversion from 'int' to 'signed char' changes value from '747' to '-21' [-Woverflow]
   23 |   st.d=747;///
      |        ^~~
p59.cpp:24:11: warning: overflow in conversion from 'int' to 'signed char' changes value from '747' to '-21' [-Woverflow]
   24 |   st.e.ee=747;///
      |           ^~~
root@4bbe4aab0054:/home/cpp#  ./a.out
a=C b=11 c=-747 d=-21 e.ee=-21
C++N4910. 6..17 Memory model [intro.memory]

今日はここまで。

参考資料

C++ Error Message Collection(1)does not name a type, 11 articles

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

Compare the contents of C++N4910:2022, C++N4741:2018 and C++N4606:2015

C++ sample list

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

文書履歴(document history)

ver. 0.01 初稿 20220614
ver. 0.02 番号記入 20220619

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0