Home | History | Annotate | Download | only in namespace.memdef
      1 // RUN: %clang_cc1 -fsyntax-only %s -verify
      2 
      3 // C++'0x [namespace.memdef] p3:
      4 //   Every name first declared in a namespace is a member of that namespace. If
      5 //   a friend declaration in a non-local class first declares a class or
      6 //   function the friend class or function is a member of the innermost
      7 //   enclosing namespace.
      8 
      9 namespace N {
     10   struct S0 {
     11     friend struct F0;
     12     friend void f0(int);
     13     struct F0 member_func();
     14   };
     15   struct F0 { };
     16   F0 f0() { return S0().member_func(); }
     17 }
     18 N::F0 f0_var = N::f0();
     19 
     20 // Ensure we can handle attaching friend declarations to an enclosing namespace
     21 // with multiple contexts.
     22 namespace N { struct S1 { struct IS1; }; }
     23 namespace N {
     24   struct S1::IS1 {
     25     friend struct F1;
     26     friend void f1(int);
     27     struct F1 member_func();
     28   };
     29   struct F1 { };
     30   F1 f1() { return S1::IS1().member_func(); }
     31 }
     32 N::F1 f1_var = N::f1();
     33 
     34 //   The name of the friend is not found by unqualified lookup (3.4.1) or by
     35 //   qualified lookup (3.4.3) until a matching declaration is provided in that
     36 //   namespace scope (either before or after the class definition granting
     37 //   friendship). If a friend function is called, its name may be found by the
     38 //   name lookup that considers functions from namespaces and classes
     39 //   associated with the types of the function arguments (3.4.2). If the name
     40 //   in a friend declaration is neither qualified nor a template-id and the
     41 //   declaration is a function or an elaborated-type-specifier, the lookup to
     42 //   determine whether the entity has been previously declared shall not
     43 //   consider any scopes outside the innermost enclosing namespace.
     44 
     45 template<typename T> struct X0 { };
     46 struct X1 { };
     47 
     48 struct Y {
     49   template<typename T> union X0;
     50   template<typename T> friend union X0;
     51 
     52   union X1;
     53   friend union X1;
     54 };
     55 
     56 namespace N {
     57   namespace M {
     58     template<typename T> class X;
     59   }
     60 }
     61 
     62 namespace N3 {
     63   class Y {
     64     template<typename T> friend class N::M::X;
     65   };
     66 }
     67 
     68 // FIXME: Woefully inadequate for testing
     69 
     70 // Friends declared as template-ids aren't subject to the restriction
     71 // on innermost namespaces.
     72 // rdar://problem/8552377
     73 namespace test5 {
     74   template <class T> void f(T);
     75   namespace ns {
     76     class A {
     77       friend void f<int>(int);
     78       static void foo(); // expected-note 2 {{declared private here}}
     79     };
     80 
     81     // Note that this happens without instantiation.
     82     template <class T> void f(T) {
     83       A::foo(); // expected-error {{'foo' is a private member of 'test5::ns::A'}}
     84     }
     85   }
     86 
     87   template <class T> void f(T) {
     88     ns::A::foo(); // expected-error {{'foo' is a private member of 'test5::ns::A'}}
     89   }
     90 
     91   template void f<int>(int);
     92   template void f<long>(long); //expected-note {{instantiation}}
     93 }
     94