N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(4)1.9 Program execution

はじめに

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++が標準化の動きとの時間的な擦れの確認が結果としてできれば幸いです。

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)

g++-7 --version

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

1.9 Program execution

p9.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf p.9

#include <iostream>

int main(){

  // p9 from here
  int a, b;
  /* ... */
  a = a + 32760 + b + 5;
  std::cout << "a=" << a << " b=" << b << std::endl; 

  a = (((a + 32760) + b) + 5);
  std::cout << "a=" << a << "b=" << b << std::endl; 

  a = ((a + b) + 32765);
  std::cout << "a=" << a << "b=" << b << std::endl; 

  a = ((a + 32765) + b);
  std::cout << "a=" << a << "b=" << b << std::endl; 

  a = (a + (b + 32765));
  std::cout << "a=" << a << "b=" << b << std::endl; 

  // p9 to here

  return EXIT_SUCCESS;
}
shell
$ ./cppgl.sh p9
$ clang++ p9.cpp
a=-342681709 b=32766
a=-342616178 b=32766
a=-342550647 b=32766
a=-342485116 b=32766
a=-342419585 b=32766

$ g++-7 p9.cpp
a=132616656 b=132583890
a=265233311 b=132583890
a=397849966 b=132583890
a=530466621 b=132583890
a=663083276 b=132583890

p10.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf p.10

#include <iostream>

  // p10 from here
struct S {
  S(int i): I(i) { }
    int& v() { return I; }
  private:
    int I;
};
S s1(1); // full-expression is call of S::S(int)
S s2 = 2; // full-expression is call of S::S(int)
void f() {
  if (S(3).v()) // full-expression includes lvalue-to-rvalue and
  // int to bool conversions, performed before
  // temporary is deleted at end of full-expression
  { 
   std::cout << "S(3).v() =" << S(3).v() <<std::endl; 
  }
}

int main(){
  f();
  return EXIT_SUCCESS;
}
shell
./cppgl.sh p10
$ clang++ p10.cpp
S(3).v() =3

$ g++-7 p10.cpp
S(3).v() =3

p11.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf p.11

#include <iostream>

  // p11 from here

void f(int, int);
void g(int i, int* v) {
  i = v[i++]; // the behavior is undefined
   std::cout << "i =" << i <<std::endl; 
  i = 7, i++, i++; // i becomes 9
   std::cout << "i =" << i <<std::endl; 
  i = i++ + 1; // the behavior is undefined
   std::cout << "i =" << i <<std::endl; 
  i = i + 1; // the value of i is incremented
   std::cout << "i =" << i <<std::endl; 
  f(i = -1, i = -1); // the behavior is undefined
}
int main(){
  int i=1;
  int j=2;
  int *k =&j;
  g(i,k);
  return EXIT_SUCCESS;
}
shell
./cppgl.sh p11
$ clang++ p11.cpp
p11.cpp:10:10: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
  i = v[i++]; // the behavior is undefined
    ~    ^
p11.cpp:14:8: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
  i = i++ + 1; // the behavior is undefined
    ~  ^
p11.cpp:18:7: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
  f(i = -1, i = -1); // the behavior is undefined
      ^       ~
3 warnings generated.
Undefined symbols for architecture x86_64:
  "f(int, int)", referenced from:
      g(int, int*) in p11-c85799.o
ld: symbol(s) not found for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)

$ g++-7 p11.cpp
Undefined symbols for architecture x86_64:
  "f(int, int)", referenced from:
      g(int, int*) in ccM1wCpf.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

参考資料

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

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

文書履歴

0.10 初稿 20180408
0.11 Add list 20190409
0.12 change the title and add URL. 20190414

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.