summaryrefslogtreecommitdiffstats
path: root/test/CXX/class.access/p6.cpp
blob: 734a4d8c486922b7679c9e27737ec6faaa07df88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// RUN: %clang_cc1 -fsyntax-only -verify %s

// C++0x [class.access]p6:
//   All access controls in [class.access] affect the ability to
//   access a class member name from a particular scope. For purposes
//   of access control, the base-specifiers of a class and the
//   definitions of class members that appear outside of the class
//   definition are considered to be within the scope of that
//   class. In particular, access controls apply as usual to member
//   names accessed as part of a function return type, even though it
//   is not possible to determine the access privileges of that use
//   without first parsing the rest of the function
//   declarator. Similarly, access control for implicit calls to the
//   constructors, the conversion functions, or the destructor called
//   to create and destroy a static data member is performed as if
//   these calls appeared in the scope of the member's class.

struct Public {}; struct Protected {}; struct Private {};

namespace test0 {
  class A {
    typedef int type; // expected-note {{declared private here}}
    type foo();
  };

  A::type foo() { } // expected-error {{'type' is a private member}}
  A::type A::foo() { }
}

// conversion decls
namespace test1 {
  class A {
  public:
    A();
    operator Public ();
    A(Public);
  protected:
    operator Protected (); // expected-note {{declared protected here}}
    A(Protected); // expected-note {{declared protected here}}
  private:
    operator Private (); // expected-note {{declared private here}}
    A(Private); // expected-note {{declared private here}}
  };

  void test() {
    A a;
    Public pub = a;
    Protected prot = a; // expected-error {{'operator Protected' is a protected member}}
    Private priv = a; // expected-error {{'operator Private' is a private member}}
    A apub = pub;
    A aprot = prot; // expected-error {{protected constructor}}
    A apriv = priv; // expected-error {{private constructor}}
  }
}

// PR6967
namespace test2 {
  class A {
  public:
    template <class T> static void set(T &t, typename T::type v) {
      t.value = v;
    }
    template <class T> static typename T::type get(const T &t) {
      return t.value;
    }
  };

  class B {
    friend class A;

  private:
    typedef int type;
    type value;
  };

  int test() {
    B b;
    A::set(b, 0);
    return A::get(b);
  }
}

namespace test3 {
  class Green {}; class Blue {};

  // We have to wrap this in a class because a partial specialization
  // isn't actually in the context of the template.
  struct Outer {
    template <class T, class Nat> class A {
    };
  };

  template <class T> class Outer::A<T, typename T::nature> {
  public:
    static void foo();
  };

  class B {
  private: typedef Green nature;
    friend class Outer;
  };

  void test() {
    Outer::A<B, Green>::foo();
    Outer::A<B, Blue>::foo(); // expected-error {{no member named 'foo'}}
  }
}

namespace test4 {
  template <class T> class A {
  private: typedef int type;
    template <class U> friend void foo(U &, typename U::type);
  };

  template <class U> void foo(U &, typename U::type) {}
  
  void test() {
    A<int> a;
    foo(a, 0);
  }
}
OpenPOWER on IntegriCloud