Search code examples
c++gccclangfriendcrtp

Template friendship error compilation with GCC but not with clang


This code compiles with clang 3.7.1 (with no diagnostic) but fails with GCC 5.3.0 (live example):

#include <iostream>

template<typename T>
struct A {
    void foo()
    {
        static_cast<T*>(this)->implementation();
    }
};

struct Crtp : A<Crtp> {
    template<typename T>
    friend struct A;
private:
    void implementation() { std::cout << "implementation()\n"; }
};

int main()
{
    Crtp c;
    c.foo();
}

GCC's error message is the following:

main.cpp:13:16: error: specialization of 'A' after instantiation friend struct A;

Which one is right and why? Is it a bug of GCC / clang?


Solution

  • Seems to be an old g++ bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52625).

    Reported and never corrected, if I understand correctly,