0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++N4741(88)10.3.3 The using declaration [namespace.udecl]p164

Last updated at Posted at 2018-05-22

#はじめに(Introduction)
C++N4741 Working Draft, Standard for Programming Language C++
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4741.pdf

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

##背景(back ground)
C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

この半年の間で、三度、自分のネットでの記録に助けられたことがあります。
また過去に解決できなかった記録を10種類以上、最近になって解決できたことがあります。それは、主に次の4つの情報に基づいています。

https://stackoverflow.com
https://cpprefjp.github.io
http://ja.cppreference.com/
https://teratail.com/

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

##作業方針(sequence)
Clang++では-std=c++03, c++17, C++2aの3種類
g++では-std=c++03, c++17の2種類
でコンパイルし、

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

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

初めての CEDD(Compile Error Driven Design) 8回直してコンパイル。
https://qiita.com/kaizen_nagoya/items/9494236aa1753f3fd1e1

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

C++N4606, 2016符号断片編纂一覧(example code compile list)

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

##C++N3242, 2011 sample code compile list on clang++ and g++
https://qiita.com/kaizen_nagoya/items/685b5c1a2c17c1bf1318

編纂器(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.

#(88)10.3.3 The using declaration [namespace.udecl]p164

C++N4606, 2016(80)7.3.3 The using declaration [namespace.udecl]p182
https://qiita.com/kaizen_nagoya/items/35335d5f2d9835bd8786

C++N3242, 2011 (64) 7.3.3 The using declaration
https://researchmap.jp/jo3x4ikyx-1797580/#_1797580

##p164.cpp
###算譜(source code)

p164.cpp
// C++N4741 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n741.pdf
const char* msg= "C++N4741(88)10.3.3 The using declaration [namespace.udecl]p164.cpp";
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
#include <cstdlib>

using namespace std;

namespace N {
struct B {
  void f(char);
  void g(char);
  enum E { e };
  union {
    int x;
  };
};
struct D : B {
  using B::f;
  void f(int) {
    f('c');  // calls B::f(char)
  }
  void g(int) {
    g('c');  // recursively calls D::g(int)
  }
};

template <typename... bases>
struct X : bases... {
  using bases::g...;
};
X<B, D> x; // OK: B::g and D::g introduced

class C {
  int g();
};
class D2 : public B {
  using B::f; // OK: B is a base of D2
  using B::e; // OK: e is an enumerator of base B
  using B::x; // OK: x is a union member of base B
  using C::g; // error: C isn’t a base of D2
};
}

namespace M {
struct A {
  template <class T> void f(T);
  template <class T> struct X { };
};
struct B : A {
  using A::f<double>; // ill-formed
  using A::X<int>; // ill-formed
};

struct X {
  int i;
  static int s;
};
void f() {
  using X::i; // error: X::i is a class member and this is not a member declaration.
  using X::s; // error: X::s is a class member and this is not a member declaration.
}
}

///namespace OX{
void f();
namespace A {
void g();
}
namespace X {
using ::f; // global f
using A::g; // A’s g
}
void h()
{
  X::f(); // calls ::f
  X::g(); // calls A::g
}
///}

namespace O {
namespace A {
int i;
}
namespace A1 {
using A::i, A::i; // OK: double declaration
}
struct B {
  int i;
};
struct X : B {
  using B::i, B::i; // error: double member declaration
};

namespace A {
void f(int);
}
using A::f; // f is a synonym for A::f; that is, for A::f(int).
namespace A {
void f(char);
}
void foo() {
  f('a'); // calls f(int), even though f(char) exists.
}
void bar() {
  using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
  f('a'); // calls f(char)
}
}

namespace P {
namespace A {
int x;
}
namespace B {
int i;
struct g { };
struct x { };
void f(int);
void f(double);
void g(char); // OK: hides struct g
}
void func() {
  int i;
  using B::i; // error: i declared twice
  void f(char);
  using B::f; // OK: each f is a function
  f(3.5); // calls B::f(double)
  using B::g;
  g('a'); // calls B::g(char)
  struct g g1; // g1 has class type B::g
  using B::x;
  using A::x; // OK: hides struct B::x
  x = 99; // assigns to A::x
  struct x x1; // x1 has class type B::x
}
}

namespace Q {
namespace B {
void f(int);
void f(double);
}
namespace C {
void f(int);
void f(double);
void f(char);
}
void h() {
  using B::f; // B::f(int) and B::f(double)
  using C::f; // C::f(int), C::f(double), and C::f(char)
  f('h'); // calls C::f(char)
  f(1); // error: ambiguous: B::f(int) or C::f(int)?
  void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
}
}

namespace R {
struct B {
  virtual void f(int);
  virtual void f(char);
  void g(int);
  void h(int);
};
struct D : B {
  using B::f;
  void f(int); // OK: D::f(int) overrides B::f(int);
  using B::g;
  void g(char); // OK
  using B::h;
  void h(int); // OK: D::h(int) hides B::h(int)
};
void k(D* p)
{
  p->f(1); // calls D::f(int)
  p->f('a'); // calls B::f(char)
  p->g(1); // calls B::g(int)
  p->g('a'); // calls D::g(char)
}
struct B1 {
  B1(int);
};
struct B2 {
  B2(int);
};
struct D1 : B1, B2 {
  using B1::B1;
  using B2::B2;
};
D1 d1(0); // ill-formed: ambiguous
struct D2 : B1, B2 {
  using B1::B1;
  using B2::B2;
  D2(int); // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
};
D2 d2(0); // calls D2::D2(int)
}

namespace S {
struct A {
  int x();
};
struct B : A { };
struct C : A {
  using A::x;
  int x(int);
};
struct D : B, C {
  using C::x;
  int x(double);
};
int f(D* d) {
  return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
}
}

namespace U {
class A {
private:
  void f(char);
public:
  void f(int);
protected:
  void g();
};
class B : public A {
  using A::f; // error: A::f(char) is inaccessible
public:
  using A::g; // B::g is a public synonym for A::g
};
}

int main() {
  cout<< msg << endl;
  return EXIT_SUCCESS;
}

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

cpla.sh
$ ../c1.sh p164
$ clang++ p164.cpp  -std=c++2a -Wall
p164.cpp:21:13: warning: all paths through this function will call itself [-Winfinite-recursion]
void g(int) { g('c'); } // recursively calls D::g(int)
            ^
p164.cpp:25:12: warning: direct base 'N::B' is inaccessible due to ambiguity:
    struct N::X<struct N::B, struct N::D> -> struct N::B
    struct N::X<struct N::B, struct N::D> -> struct N::D -> struct N::B [-Winaccessible-base]
struct X : bases... {
           ^~~~~
p164.cpp:28:9: note: in instantiation of template class 'N::X<N::B, N::D>' requested here
X<B, D> x; // OK: B::g and D::g introduced
        ^
p164.cpp:37:7: error: using declaration refers into 'C::', which is not a base class of 'D2'
using C::g; // error: C isn’t a base of D2
      ^~~
p164.cpp:47:10: error: using declaration cannot refer to a template specialization
using A::f<double>; // ill-formed
         ^~~~~~~~~
p164.cpp:48:10: error: using declaration cannot refer to a template specialization
using A::X<int>; // ill-formed
         ^~~~~~
p164.cpp:56:10: error: using declaration cannot refer to class member
using X::i; // error: X::i is a class member and this is not a member declaration.
      ~~~^
p164.cpp:57:10: error: using declaration cannot refer to class member
using X::s; // error: X::s is a class member and this is not a member declaration.
      ~~~^
p164.cpp:57:1: note: use a reference instead
using X::s; // error: X::s is a class member and this is not a member declaration.
^~~~~
auto &s = 
p164.cpp:88:16: error: redeclaration of using declaration
using B::i, B::i; // error: double member declaration
            ~~~^
p164.cpp:88:10: note: previous using declaration
using B::i, B::i; // error: double member declaration
         ^
p164.cpp:121:10: error: target of using declaration conflicts with declaration already in scope
using B::i; // error: i declared twice
         ^
p164.cpp:112:5: note: target of using declaration
int i;
    ^
p164.cpp:120:5: note: conflicting declaration
int i;
    ^
p164.cpp:149:1: error: call to 'f' is ambiguous
f(1); // error: ambiguous: B::f(int) or C::f(int)?
^
p164.cpp:137:6: note: candidate function
void f(int);
     ^
p164.cpp:141:6: note: candidate function
void f(int);
     ^
p164.cpp:138:6: note: candidate function
void f(double);
     ^
p164.cpp:142:6: note: candidate function
void f(double);
     ^
p164.cpp:143:6: note: candidate function
void f(char);
     ^
p164.cpp:150:6: error: declaration conflicts with target of using declaration already in scope
void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
     ^
p164.cpp:141:6: note: target of using declaration
void f(int);
     ^
p164.cpp:147:10: note: using declaration
using C::f; // C::f(int), C::f(double), and C::f(char)
         ^
p164.cpp:186:4: error: call to constructor of 'R::D1' is ambiguous
D1 d1(0); // ill-formed: ambiguous
   ^  ~
p164.cpp:177:1: note: candidate inherited constructor
B1(int);
^
p164.cpp:183:11: note: constructor from base class 'B1' inherited here
using B1::B1;
          ^
p164.cpp:180:1: note: candidate inherited constructor
B2(int);
^
p164.cpp:184:11: note: constructor from base class 'B2' inherited here
using B2::B2;
          ^
p164.cpp:182:8: note: candidate is the implicit copy constructor
struct D1 : B1, B2 {
       ^
p164.cpp:182:8: note: candidate is the implicit move constructor
p164.cpp:207:8: error: ambiguous conversion from derived class 'S::D' to base class 'S::A':
    struct S::D -> struct S::B -> struct S::A
    struct S::D -> struct S::C -> struct S::A
return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
       ^
p164.cpp:221:10: error: 'f' is a private member of 'U::A'
using A::f; // error: A::f(char) is inaccessible
         ^
p164.cpp:214:6: note: declared private here
void f(char);
     ^
2 warnings and 12 errors generated.

##p164a.cpp
コンパイルエラーを取る。
下記3段階でリンクエラーが出て、順次{;}等を入れる。

Undefined symbols for architecture x86_64:
  "f()", referenced from:
      h() in p164a-23b5e4.o
  "A::g()", referenced from:
      h() in p164a-23b5e4.o
  "O::A::f(char)", referenced from:
      O::bar() in p164a-23b5e4.o
  "O::A::f(int)", referenced from:
      O::foo() in p164a-23b5e4.o
  "P::B::f(double)", referenced from:
      P::func() in p164a-23b5e4.o
  "P::B::g(char)", referenced from:
      P::func() in p164a-23b5e4.o
  "Q::C::f(char)", referenced from:
      Q::h() in p164a-23b5e4.o
  "R::B::g(int)", referenced from:
      R::k(R::D*) in p164a-23b5e4.o
  "R::D::g(char)", referenced from:
      R::k(R::D*) in p164a-23b5e4.o
  "R::D2::D2(int)", referenced from:
      ___cxx_global_var_init in p164a-23b5e4.o
ld: symbol(s) not found for architecture x86_64
p164a.cpp:190:1: error: constructor for 'R::D2' must explicitly initialize the base class
      'R::B1' which does not have a default constructor
D2(int){;} // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
^
p164a.cpp:176:8: note: 'R::B1' declared here
struct B1 {
       ^
p164a.cpp:190:1: error: constructor for 'R::D2' must explicitly initialize the base class
      'R::B2' which does not have a default constructor
D2(int){;} // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
Undefined symbols for architecture x86_64:
  "A::g()", referenced from:
      h() in p164a-b9a245.o
  "O::A::f(int)", referenced from:
      O::foo() in p164a-b9a245.o
ld: symbol(s) not found for architecture x86_64

###算譜(code)

p164a.cpp
// C++N4741 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n741.pdf
const char* msg= "C++N4741(88)10.3.3 The using declaration [namespace.udecl]p164a.cpp";
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
#include <cstdlib>

using namespace std;

namespace N {
struct B {
  void f(char);
  void g(char);
  enum E { e };
  union {
    int x;
  };
};
struct D : B {
  using B::f;
  void f(int) {
    f('c');  // calls B::f(char)
  }
  void g(int) {
    g('c');  // recursively calls D::g(int)
  }
};

template <typename... bases>
struct X : bases... {
  using bases::g...;
};
X<B, D> x; // OK: B::g and D::g introduced

class C {
  int g();
};
class D2 : public B {
  using B::f; // OK: B is a base of D2
  using B::e; // OK: e is an enumerator of base B
  using B::x; // OK: x is a union member of base B
///using C::g; // error: C isn’t a base of D2
};
}

namespace M {
struct A {
  template <class T> void f(T);
  template <class T> struct X { };
};
struct B : A {
///using A::f<double>; // ill-formed
///using A::X<int>; // ill-formed
};

struct X {
  int i;
  static int s;
};
void f() {
///using X::i; // error: X::i is a class member and this is not a member declaration.
///using X::s; // error: X::s is a class member and this is not a member declaration.
}
}

///namespace OX{
void f() {;}
namespace A {
void g() {;}
}
namespace X {
using ::f; // global f
using A::g; // A’s g
}
void h()
{
  X::f(); // calls ::f
  X::g(); // calls A::g
}
///}

namespace O {
namespace A {
int i;
}
namespace A1 {
using A::i, A::i; // OK: double declaration
}
struct B {
  int i;
};
struct X : B {
///using B::i, B::i; // error: double member declaration
};

namespace A {
void f(int) {;}
}
using A::f; // f is a synonym for A::f; that is, for A::f(int).
namespace A {
void f(char) {;}
}
void foo() {
  f('a'); // calls f(int), even though f(char) exists.
}
void bar() {
  using A::f; // f is a synonym for A::f; that is, for A::f(int) and A::f(char).
  f('a'); // calls f(char)
}
}

namespace P {
namespace A {
int x;
}
namespace B {
int i;
struct g { };
struct x { };
void f(int);
void f(double) {;}
void g(char) {;} // OK: hides struct g
}
void func() {
  int i;
///using B::i; // error: i declared twice
  void f(char);
  using B::f; // OK: each f is a function
  f(3.5); // calls B::f(double)
  using B::g;
  g('a'); // calls B::g(char)
  struct g g1; // g1 has class type B::g
  using B::x;
  using A::x; // OK: hides struct B::x
  x = 99; // assigns to A::x
  struct x x1; // x1 has class type B::x
}
}

namespace Q {
namespace B {
void f(int);
void f(double);
}
namespace C {
void f(int);
void f(double);
void f(char) {;}
}
void h() {
  using B::f; // B::f(int) and B::f(double)
  using C::f; // C::f(int), C::f(double), and C::f(char)
  f('h'); // calls C::f(char)
///f(1); // error: ambiguous: B::f(int) or C::f(int)?
///void f(int); // error: f(int) conflicts with C::f(int) and B::f(int)
}
}

namespace R {
struct B {
  virtual void f(int);
  virtual void f(char);
  void g(int) {;}
  void h(int);
};
struct D : B {
  using B::f;
  void f(int); // OK: D::f(int) overrides B::f(int);
  using B::g;
  void g(char) {;} // OK
  using B::h;
  void h(int); // OK: D::h(int) hides B::h(int)
};
void k(D* p)
{
  p->f(1); // calls D::f(int)
  p->f('a'); // calls B::f(char)
  p->g(1); // calls B::g(int)
  p->g('a'); // calls D::g(char)
}
struct B1 {
  B1() {;}
  B1(int);
};
struct B2 {
  B2() {;}
  B2(int);
};
struct D1 : B1, B2 {
  using B1::B1;
  using B2::B2;
};
///D1 d1(0); // ill-formed: ambiguous
struct D2 : B1, B2 {
  using B1::B1;
  using B2::B2;
  D2(int) {;} // OK: D2::D2(int) hides B1::B1(int) and B2::B2(int)
};
D2 d2(0); // calls D2::D2(int)
}

namespace S {
struct A {
  int x();
};
struct B : A { };
struct C : A {
  using A::x;
  int x(int);
};
struct D : B, C {
  using C::x;
  int x(double);
};
int f(D* d) {
///return d->x(); // error: overload resolution selects A::x, but A is an ambiguous base class
}
}

namespace U {
class A {
private:
  void f(char);
public:
  void f(int);
protected:
  void g();
};
class B : public A {
///using A::f; // error: A::f(char) is inaccessible
public:
  using A::g; // B::g is a public synonym for A::g
};
}

int main() {
  cout<< msg << endl;
  return EXIT_SUCCESS;
}

#検討事項(agenda)
役に立つまたは意味のあるその他の出力

#参考資料(reference)

docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de

C++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

C++2003とC++2017でコンパイルエラーになるならない事例集
https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff

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

cpprefjpのdecltypeをコンパイル試験
https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67

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

#文書履歴(document history)
ver. 0.10 初稿 20180523

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?