#はじめに
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.
#(40)5.2.7 Dynamic cast [expr.dynamic.cast]
p111
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf
#define msg "p111.cpp (40)5.2.7 Dynamic cast [expr.dynamic.cast]"
#include <iostream>
struct B1 { };
struct D1 : B1 { };
void foo(D1* dp) {
B1* bp = dynamic_cast<B1*>(dp); // equivalent to B1* bp = dp;
}
class A { virtual void f(); };
class B { virtual void g(); };
class D : public virtual A, private B { };
void g() {
D d;
B* bp = (B*)&d; // cast needed to break protection
A* ap = &d; // public derivation, no cast needed
D& dr = dynamic_cast<D&>(*bp); // fails
ap = dynamic_cast<A*>(bp); // fails
bp = dynamic_cast<B*>(ap); // fails
ap = dynamic_cast<A*>(&d); // succeeds
bp = dynamic_cast<B*>(&d); // ill-formed (not a run-time check)
}
class E : public D, public B { };
class F : public E, public D { };
void h() {
F f;
A* ap = &f; // succeeds: finds unique A
D* dp = dynamic_cast<D*>(ap); // fails: yields 0
// f has two D subobjects
E* ep = (E*)ap; // ill-formed: cast from virtual base
E* ep1 = dynamic_cast<E*>(ap); // succeeds
}
int main(int argc, char *argv[], char *envp[]){
std::cout << msg << std::endl;
return EXIT_SUCCESS;
}
$ ./cppgl17.sh p111
$ clang++ p111.cpp
p111.cpp:24:6: error: cannot cast 'D' to its private base class 'B'
bp = dynamic_cast<B*>(&d); // ill-formed (not a run-time check)
^
p111.cpp:15:29: note: declared private here
class D : public virtual A, private B { };
^~~~~~~~~
p111.cpp:26:21: warning: direct base 'B' is inaccessible due to ambiguity:
class E -> class D -> class B
class E -> class B [-Winaccessible-base]
class E : public D, public B { };
^~~~~~~~
p111.cpp:27:21: warning: direct base 'D' is inaccessible due to ambiguity:
class F -> class E -> class D
class F -> class D [-Winaccessible-base]
class F : public E, public D { };
^~~~~~~~
p111.cpp:33:9: error: cannot cast 'A *' to 'E *' via virtual base 'A'
E* ep = (E*)ap; // ill-formed: cast from virtual base
^~~~~~
2 warnings and 2 errors generated.
$ g++-7 p111.cpp
p111.cpp: In function 'void g()':
p111.cpp:24:25: error: 'B' is an inaccessible base of 'D'
bp = dynamic_cast<B*>(&d); // ill-formed (not a run-time check)
^
p111.cpp:24:25: error: 'B' is an inaccessible base of 'D'
p111.cpp: At global scope:
p111.cpp:26:7: warning: direct base 'B' inaccessible in 'E' due to ambiguity
class E : public D, public B { };
^
p111.cpp:27:7: warning: direct base 'D' inaccessible in 'F' due to ambiguity
class F : public E, public D { };
^
p111.cpp: In function 'void h()':
p111.cpp:33:13: error: cannot convert from pointer to base class 'A' to pointer to derived class 'E' because the base is virtual
E* ep = (E*)ap; // ill-formed: cast from virtual base
^~
#検討事項
コンパイルエラーにならない修正。
内容の出力。
g++とclang++のエラーの違いの説明。
#参考資料
コンパイル用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 初稿 2080415