Is it possible to invoke an injected friend template function?

℡╲_俬逩灬. 提交于 2019-12-08 09:33:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/54158325/is-it-possible-to-invoke-an-injected-friend-template-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!