Template class friend in anonymous namespace

When a class is declared Aas a friend of the class B, but Ais defined inside the anonymous namespace and Bexternally, some compilers produce a "protected member is not available" error, while others do not produce any error or warning. The situation changes if either A, or B, or both are patterns:

namespace {
  template <class T>
  struct A {
    template <class BB>
    void foo(BB const& b) { b.bar(); }
  };
} // end anonymous namespace

template <class T>
class B {
  template <class> friend struct A;

protected:
  void bar() const {}
};

int main() {
  A<int> a;
  a.foo(B<int>{});
}
  • Aand B- both patterns. Then Intel icc 18: error #308: function "B<T>::bar [with T=int]" is inaccessible , gcc 7.2: no error, clang 5.0: no error
  • Ais not a template, but Ba template: Intel icc 18: no error, gcc 7.2: error :, 'void B<T>::bar() const [with T = int]' is protected within this contextclang 5: no error
  • A , B : Intel icc 18: #308, gcc 7.2: , clang 5: no error
  • A, B : Intel icc 18: , gcc 7.2: , clang 5: no error
  • A, B ( 4.), A B: Intel icc 18: #308, gcc 7.2: , clang 5: : 'bar' is a protected member of 'B'
  • A B ( 1.), A B: Intel icc 18: #308, gcc 7.2: , clang 5:

. : https://godbolt.org/g/6Zdr3c https://godbolt.org/g/BRqf78 6.

, ? ?

+4
1

, 1-4 :

(7.3.1.1:1) , , , using.

A 11.3: 9

, , , .

, A B B::bar

5 . , A , A .

+1

Source: https://habr.com/ru/post/1692231/


All Articles