N4606 Working Draft 2016, ISO/IEC 14882, C++ standard(19)3.4.3.2 Namespace members[namespace.qual] p.55

はじめに

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.

(19)3.4.3.2 Namespace members[namespace.qual]

p.55

p55.cpp
// N4606 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf 
#define msg "p55.cpp (19)3.4.3.2 Namespace members[namespace.qual]"

#include <iostream>

int x;
namespace Y {
  void f(float);
  void h(int);
}
namespace Z {
  void h(double);
}
namespace A {
  using namespace Y;
  void f(int);
  void g(int);
  int i;
}
namespace B {
  using namespace Z;
  void f(char);
  int i;
}
namespace AB {
  using namespace A;
  using namespace B;
  void g();
}
void h()
{
  AB::g(); // g is declared directly in AB,
  // therefore S is { AB::g() } and AB::g() is chosen
  AB::f(1); // f is not declared directly in AB so the rules are
  // applied recursively to A and B;
  // namespace Y is not searched and Y::f(float)
  // is not considered;
  // S is { A::f(int), B::f(char) } and overload
  // resolution chooses A::f(int)
  AB::f('c'); // as above but resolution chooses B::f(char)
  AB::x++; // x is not declared directly in AB, and
  // is not declared in A or B , so the rules are
  // applied recursively to Y and Z,
  // S is { } so the program is ill-formed
  AB::i++; // i is not declared directly in AB so the rules are
  // applied recursively to A and B,
  // S is { A::i , B::i } so the use is ambiguous
  // and the program is ill-formed
  AB::h(16.8); // h is not declared directly in AB and
  // not declared directly in A or B so the rules are
  // applied recursively to Y and Z,
  // S is { Y::h(int), Z::h(double) } and overload
  // resolution chooses Z::h(double)
}

namespace A {
  int a;
}
namespace B {
  using namespace A;
}
namespace C {
  using namespace A;
}
namespace BC {
  using namespace B;
  using namespace C;
}
void f()
{
  BC::a++; // OK: S is { A::a, A::a }
}
namespace D {
  using A::a;
}
namespace BD {
  using namespace B;
  using namespace D;
}
void g()
{
  BD::a++; // OK: S is { A::a, A::a }
}

namespace B {
  int b;
}
namespace A {
  using namespace B;
  int a;
}
namespace B {
  using namespace A;
}
void f()
{
  A::a++; // OK: a declared directly in A, S is { A::a}
  B::a++; // OK: both A and B searched (once), S is { A::a}
  A::b++; // OK: both A and B searched (once), S is { B::b}
  B::b++; // OK: b declared directly in B, S is { B::b}
}

namespace A {
  struct x { };
  int x;
  int y;
}
namespace B {
  struct y { };
}
namespace C {
  using namespace A;
  using namespace B;
  int i = C::x; // OK, A::x (of type int )
  int j = C::y; // ambiguous, A::y or B::y
}

namespace A {
  namespace B {
    void f1(int);
  }
  using namespace B;
}
void A::f1(int){ } // ill-formed, f1 is not a member of A

namespace A {
  namespace B {
    void f1(int);
  }
}
namespace C {
  namespace D {
    void f1(int);
  }
}
using namespace A;
using namespace C::D;
void B::f1(int){ } // OK, defines A::B::f1(int)

int main(int argc, char *argv[], char *envp[]){
  std::cout << msg << std::endl;
  return EXIT_SUCCESS;
}
./cppgl.sh p55
$ clang++ p55.cpp
p55.cpp:42:3: error: no member named 'x' in namespace 'AB'; did you mean simply 'x'?
  AB::x++; // x is not declared directly in AB, and
  ^~~~~
  x
p55.cpp:7:5: note: 'x' declared here
int x;
    ^
p55.cpp:46:7: error: reference to 'i' is ambiguous
  AB::i++; // i is not declared directly in AB so the rules are
  ~~~~^
p55.cpp:24:7: note: candidate found by name lookup is 'B::i'
  int i;
      ^
p55.cpp:19:7: note: candidate found by name lookup is 'A::i'
  int i;
      ^
p55.cpp:46:7: error: unknown type name 'i'
  AB::i++; // i is not declared directly in AB so the rules are
      ^
p55.cpp:46:8: error: expected unqualified-id
  AB::i++; // i is not declared directly in AB so the rules are
       ^
p55.cpp:91:7: error: redefinition of 'a'
  int a;
      ^
p55.cpp:58:7: note: previous definition is here
  int a;
      ^
p55.cpp:96:6: error: redefinition of 'f'
void f()
     ^
p55.cpp:70:6: note: previous definition is here
void f()
     ^
p55.cpp:116:14: error: a type named 'y' is hidden by a declaration in a different namespace
  int j = C::y; // ambiguous, A::y or B::y
          ~~~^
p55.cpp:110:10: note: type declaration hidden
  struct y { };
         ^
p55.cpp:107:7: note: declaration hides type
  int y;
      ^
p55.cpp:125:6: error: out-of-line definition of 'f1' does not match any declaration in namespace 'A'; did you mean 'A::B::f1'?
void A::f1(int){ } // ill-formed, f1 is not a member of A
     ^~~~~
     A::B::f1
p55.cpp:121:10: note: 'A::B::f1' declared here
    void f1(int);
         ^
p55.cpp:139:6: error: reference to 'B' is ambiguous
void B::f1(int){ } // OK, defines A::B::f1(int)
     ^
p55.cpp:109:11: note: candidate found by name lookup is 'B'
namespace B {
          ^
p55.cpp:128:13: note: candidate found by name lookup is 'A::B'
  namespace B {
            ^
9 errors generated.

$ g++-7 p55.cpp
p55.cpp: In function 'void h()':
p55.cpp:42:7: error: 'x' is not a member of 'AB'
   AB::x++; // x is not declared directly in AB, and
       ^
p55.cpp:42:7: note: suggested alternative:
p55.cpp:7:5: note:   'x'
 int x;
     ^
p55.cpp:46:3: error: reference to 'i' is ambiguous
   AB::i++; // i is not declared directly in AB so the rules are
   ^~
p55.cpp:24:7: note: candidates are: int B::i
   int i;
       ^
p55.cpp:19:7: note:                 int A::i
   int i;
       ^
p55.cpp: At global scope:
p55.cpp:91:7: error: redefinition of 'int A::a'
   int a;
       ^
p55.cpp:58:7: note: 'int A::a' previously declared here
   int a;
       ^
p55.cpp: In function 'void f()':
p55.cpp:96:6: error: redefinition of 'void f()'
 void f()
      ^
p55.cpp:70:6: note: 'void f()' previously defined here
 void f()
      ^
p55.cpp: At global scope:
p55.cpp:116:11: error: reference to 'y' is ambiguous
   int j = C::y; // ambiguous, A::y or B::y
           ^
p55.cpp:110:10: note: candidates are: struct B::y
   struct y { };
          ^
p55.cpp:107:7: note:                 int A::y
   int y;
       ^
p55.cpp:125:15: error: 'void A::B::f1(int)' should have been declared inside 'A'
 void A::f1(int){ } // ill-formed, f1 is not a member of A
               ^
p55.cpp:139:6: error: reference to 'B' is ambiguous
 void B::f1(int){ } // OK, defines A::B::f1(int)
      ^
p55.cpp:21:11: note: candidates are: namespace B { }
 namespace B {
           ^
p55.cpp:120:13: note:                 namespace A::B { }
   namespace B {
             ^

検討事項

 コンパイルエラーにならない変更。
 内容の出力。

参考資料

コンパイル用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

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