Search code examples
c++c++11friendfunction-templates

Is it possible to invoke an injected friend template function?


To be consistent with other (non-template) functions in a class I wanted to define and invoke a friend template function.

I can define it with no problem (see function t below).

namespace ns{
struct S{
    void m() const{}
    friend void f(S const&){}
    template<class T>
    friend void t(S const&){}
};
template<class T>
void t2(S const& s){}
}

However later I am not able to invoke this t function in any way?

int main(){
    ns::S s;
    s.m();
    f(s);
//  t<int>(s); // error: ‘t’ was not declared in this scope (I was expecting this to work)
//  ns::t<int>(s); // error: ‘t’ is not a member of ‘ns’
//  ns::S::t<int>(s); // error: ‘t’ is not a member of ‘ns::S’
}

Even if it is not possible at all, I am surprised that I am allowed to define it.

I tested this with gcc 8 and clang 7.


Solution

  • What you need for this to work are a couple of forward declarations.

    The below two lines of code should come before the namespace ns.

    struct S; //Needed because S is used as a parameter in the function template
    template<class T> void t(S const&);
    

    And then this form of call will work inside main.

    t<int>(s);
    

    See demo here.