C++N4606(190)14.6 Name resolution [temp.res]p377


はじめに

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

作業方針

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

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

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.

(190)14.6 Name resolution [temp.res]p377

p377.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf
#define msg "(190)14.6 Name resolution [temp.res]p377.cpp"
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
using namespace std;

// no B declared here
class X;
template<class T> class Y {
  class Z; // forward declaration of member class
  void f() {
    X* a1; // declare pointer to X
    T* a2; // declare pointer to T
    Y* a3; // declare pointer to Y<T>
    Z* a4; // declare pointer to Z
    typedef typename T::A TA;
    TA* a5; // declare pointer to T’s A
    typename T::A* a6; // declare pointer to T’s A
    T::A* a7; // T::A is not a type name:
// multiply T::A by a7; ill-formed,
// no visible declaration of a7
    B* a8; // B is not a type name:
// multiply B by a8; ill-formed,
// no visible declarations of B and a8
  }
};

struct A2 {
  struct X2 { };
  int X2;
};
struct B2 {
  struct X2 { };
};
template<class T> void f2(T t) {
  typename T::X2 x;
}
void foo() {
  A2 a;
  B2 b;
  f2(b); // OK: T::X refers to B::X
  f2(a); // error: T::X refers to the data member A::X not the struct A2::X2
}

template <class T> void f(int i) {
  T::x * i; // T::x must not be a type
}
struct Foo {
  typedef int x;
};
struct Bar {
  static int const x = 5;
};

int main() {
  f<Bar>(1); // OK
  f<Foo>(1); // error: Foo::x is a type
  std::cout<< msg << std::endl;
  return EXIT_SUCCESS;
}

template<class T> struct A {
  typedef int B;
  B b; // OK, no typename required
};

int j;
template<class T> class X3 {
  void f3(T t, int i, char* p) {
    t = i; // diagnosed if X3::f3 is instantiated
// and the assignment to t is an error
    p = i; // may be diagnosed even if X3::f3 is
// not instantiated
    p = j; // may be diagnosed even if X3::f3 is
// not instantiated
  }
  void g(T t) {
    +; // may be diagnosed even if X::g is
// not instantiated
  }
};
template<class... T> struct A3 {
  void operator++(int, T... t); // error: too many parameters
};
template<class... T> union X3 : T... { }; // error: union with base class
template<class... T> struct A3 : T..., T... { };// error: duplicate base class

template<class T> class Set {
  T* p;
  int cnt;
public:
  Set();
  Set<T>(const Set<T>&);
  void printall() {
    for (int i = 0; i<cnt; i++)
      cout << p[i] << '\n';
  }
};

void f4(char);
template<class T> void g4(T t) {
  f4(1); // f4(char)
  f4(T(1)); // dependent
  f4(t); // dependent
  dd++; // not dependent
// error: declaration for dd not found
}
enum E { e };
void f4(E);
double dd;
void h() {
  g4(e); // will cause one call of f4(char) followed
// by two calls of f4(E)
  g4('a'); // will cause three calls of f4(char)
}

$ ./cppgl17.sh p377
$ clang++ p377.cpp
p377.cpp:21:7: error: use of undeclared identifier 'a7'
T::A* a7; // T::A is not a type name:
      ^
p377.cpp:24:1: error: unknown type name 'B'
B* a8; // B is not a type name:
^
p377.cpp:74:5: error: assigning to 'char *' from incompatible type 'int'
p = i; // may be diagnosed even if X3::f3 is
    ^
p377.cpp:76:5: error: assigning to 'char *' from incompatible type 'int'
p = j; // may be diagnosed even if X3::f3 is
    ^
p377.cpp:80:2: error: expected expression
+; // may be diagnosed even if X::g is
 ^
p377.cpp:85:6: error: overloaded 'operator++' must be a unary or binary operator (has 3 parameters)
void operator++(int, T... t); // error: too many parameters
     ^
p377.cpp:87:19: error: template type parameter pack conflicts with previous template type parameter
template<class... T> union X3 : T... { }; // error: union with base class
                  ^
p377.cpp:70:16: note: previous template type parameter declared here
template<class T> class X3 {
               ^
p377.cpp:88:29: error: redefinition of 'A3'
template<class... T> struct A3 : T..., T... { };// error: duplicate base class
                            ^
p377.cpp:84:29: note: previous definition is here
template<class... T> struct A3 {
                            ^
p377.cpp:107:1: error: use of undeclared identifier 'dd'
dd++; // not dependent
^
p377.cpp:38:13: error: typename specifier refers to non-type member 'X2' in 'A2'
typename T::X2 x;
~~~~~~~~~~~~^~
p377.cpp:44:1: note: in instantiation of function template specialization 'f2<A2>' requested here
f2(a); // error: T::X refers to the data member A::X not the struct A2::X2
^
p377.cpp:32:5: note: referenced member 'X2' is declared here
int X2;
    ^
p377.cpp:48:6: warning: expression result unused [-Wunused-value]
T::x * i; // T::x must not be a type
~~~~ ^ ~
p377.cpp:58:1: note: in instantiation of function template specialization 'f<Bar>' requested here
f<Bar>(1); // OK
^
p377.cpp:48:1: error: missing 'typename' prior to dependent type name 'Foo::x'
T::x * i; // T::x must not be a type
^~~~
p377.cpp:59:1: note: in instantiation of function template specialization 'f<Foo>' requested here
f<Foo>(1); // error: Foo::x is a type
^
1 warning and 11 errors generated.

$ g++-7 p377.cpp
p377.cpp: In member function 'void Y<T>::f()':
p377.cpp:21:7: error: 'a7' was not declared in this scope
 T::A* a7; // T::A is not a type name:
       ^~
p377.cpp:21:7: note: suggested alternative: 'a6'
 T::A* a7; // T::A is not a type name:
       ^~
       a6
p377.cpp:24:1: error: 'B' was not declared in this scope
 B* a8; // B is not a type name:
 ^
p377.cpp:24:4: error: 'a8' was not declared in this scope
 B* a8; // B is not a type name:
    ^~
p377.cpp:24:4: note: suggested alternative: 'a6'
 B* a8; // B is not a type name:
    ^~
    a6
p377.cpp: In member function 'void X3<T>::g(T)':
p377.cpp:80:2: error: expected primary-expression before ';' token
 +; // may be diagnosed even if X::g is
  ^
p377.cpp: At global scope:
p377.cpp:85:28: error: 'void A3<T>::operator++(int, T ...)' must take either zero or one argument
 void operator++(int, T... t); // error: too many parameters
                            ^
p377.cpp:70:10: error: template parameter 'class T'
 template<class T> class X3 {
          ^~~~~
p377.cpp:87:28: error: redeclared here as 'class ... T'
 template<class... T> union X3 : T... { }; // error: union with base class
                            ^~
p377.cpp:88:29: error: redefinition of 'struct A3<T>'
 template<class... T> struct A3 : T..., T... { };// error: duplicate base class
                             ^~
p377.cpp:84:29: note: previous definition of 'struct A3<T>'
 template<class... T> struct A3 {
                             ^~
p377.cpp: In function 'void g4(T)':
p377.cpp:107:1: error: 'dd' was not declared in this scope
 dd++; // not dependent
 ^~
p377.cpp: In instantiation of 'void f(int) [with T = Foo]':
p377.cpp:59:9:   required from here
p377.cpp:48:6: error: dependent-name 'T:: x' is parsed as a non-type, but instantiation yields a type
 T::x * i; // T::x must not be a type
 ~~~~~^~~
p377.cpp:48:6: note: say 'typename T:: x' if a type is meant

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

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
https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

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

文書履歴

0.10 初稿 2080420