Search code examples
c++templatesg++language-lawyerfriend

templated friend-declaration not working under g++ 5.4.0 -- compiler bug or bad code?


Below is some C++ code that compiles without error on my Mac (Xcode 10.2 10E125 / Apple LLVM version 10.0.1 (clang-1001.0.46.4)) but gives a compiler-error message (shown below) on my Linux box (g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609).

My question is, is this a compiler-bug in g++ 5.4.0, or am I doing something wrong in my code?

The compiler error from g++ 5.4.0 is:

$ g++ template_friend.cpp
template_friend.cpp: In instantiation of ‘class SubClass<int>’:
template_friend.cpp:25:23:   required from here
template_friend.cpp:10:10: error: ‘class BaseClass<int>::InnerClassFunctor’ is private
    class InnerClassFunctor
          ^
template_friend.cpp:17:26: error: within this context
 template <class T> class SubClass : public BaseClass<typename BaseClass<T>::InnerClassFunctor >

... and the code to reproduce the error is:

template<class T> class BaseClass
{
public:
   BaseClass(int) {}

private:
   template<class HisT> friend class SubClass;

   class InnerClassFunctor
   {
   public:
      InnerClassFunctor() {}
   };
};

template <class T> class SubClass : public BaseClass<typename BaseClass<T>::InnerClassFunctor >
{
public:
   SubClass(int x) : BaseClass<typename BaseClass<T>::InnerClassFunctor >(x) {/* empty */}
};

int main(int, char **)
{
   SubClass<int> table(0);
   return 0;
}

Solution

  • While the standard doesn’t address this case explicitly, I think this is a GCC bug: neither it nor Clang implement deferred access checking properly, and GCC in particular has trouble with template parameters (and, apparently, arguments).