---
# System prepended metadata

title: friend declaration does not match for template
tags: [C++]

---

There are different handling in gcc and clang for the friend function which is a template.
See the following example code [GodBolt](https://godbolt.org/z/jjo7M15c7)

If we use clang which version is bigger than 9.
The compiler will thorw out the error 
`error: friend declaration of 'foo' does not match any declaration in namespace 'abc::cde'`

However, gcc works fine in this case.

My guess is that, clang will check the foo(T, MyClass&) is declared at Line26.
But gcc check the foo(T, MyClass&) at Line42, if we remove teh Line 32~37 and we can see the error:
`error: no matching function for call to 'foo(double, MyClass&)'`
```cpp=

#include <iostream>

struct MyClass;
namespace abc {
namespace cde {
    template<typename T>
    void foo(T,int);

}
};
/* 
 * this declaration is necessary.
 * Clang will error out if removing this declaration.
 * Gcc is fine
*/
namespace abc {
namespace cde {
    template<typename T>
    void foo(T,MyClass&);

}
};

struct MyClass {
    template<typename T>
    friend void abc::cde::foo(T,MyClass&);


private:
    int mValue{3};
};
namespace abc::cde {
  template<typename T>
    void foo(T a,MyClass&) {
    std::cout<<a<<'\n';
  }
}

int main () {
    MyClass t;
    // error
     abc::cde::foo(7.2, t);
    abc::cde::foo(7, t);
    return 0;
}
```