c ++:如何调用类中定义的朋友模板函数?

pyr*_*lis 4 c++ templates

嗨大家好请帮我这个功能我从我的书中得到了这个例子,但我不知道如何实际调用票函数这是代码:

#include <iostream>
    class Manager { 
    public:
        template<typename T> 
            friend int ticket() { 
                return ++Manager::counter; 
            } 
        static int counter; 
    }; 

int main()
{
    Manager m;
    std::cout << "ticket: " << ticket<int>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我得到"候选功能不可访问"错误消息 感谢很多寻找!

Fai*_*ali 15

几点可以帮助您弄清楚这里发生了什么:

I)类中的Friend函数定义只能通过从类定义外部调用的Argument依赖查找来找到.

II)提供显式模板参数的函数模板不会经历ADL,除非编译器在将调用标识为函数调用时给出了一些明确的帮助.

III)参数相关查找(ADL)仅适用于用户定义的类型.

一些例子将更好地说明以上各点:

//------------------------
struct S 
{
  friend int f(int) { return 0; }  // 1
  friend int f(S) { return 0; }    // 2

};

S s;
int i = f(3); // error - ADL does not work for ints, (III) 
int j = f(s); // ok - ADL works for UDTs and helps find friend function - calls 2 (III)

// so how do we call function 1? If the compiler won't find the name via ADL
// declare the function in the namespace scope (since that is where the friend function
// gets injected)

int f(int);  // This function declaration refers to the same function as #1
int k = f(3); // ok - but not because of ADL 

// ok now lets add some friend templates and make this interesting
struct S 
{
  friend int f(int) { return 0; }  // 1
  friend int f(S) { return 0; }    // 2
  template<class T> friend int g(int) { return 0; } // 3
  template<class T> friend int g(S) { return 0; } // 4
  template<class T> friend int g() { return 0; } // 5
};
S s;
int k = g(5); // error - no ADL (III)
int l = g(s); // ok - ADL - calls 4
int m = g<int>(s); // should call 4 - but no ADL (point II above)

// ok so point II above says we have to give the compiler some help here
// We have to tell the compiler that g<int> identifies a function
// The way to do that is to add a visible dummy template function declaration

template<class /*Dummy*/, class /*TriggerADL*/> void g(); 

int m = g<int>(s); // ok - compiler recognizes fun call, ADL triggered - calls 4
int n = g<int>(3); // still not ok - no ADL for ints

// so how do we call either function 3 or 5 since we cannot rely on ADL?
// Remember friend functions are injected into the outer namespace
// so lets just declare the functions in the outer namespace (as we did above)
// both these declarations of g below refer to their counterparts defined in S
template<class T> int g(int);
template<class T> int g();
int o = g<int>(3); // ok
int p = g<int>(); // ok

// Of course once you have these two declarations at namespace scope
// you can get rid of the Dummy, TriggerADL declaration.
Run Code Online (Sandbox Code Playgroud)

好的,现在让我们回到您引用的Vandevoorde示例,现在这应该很简单:

#include <iostream>
class Manager { 
public:
    template<typename T> 
        friend int ticket() { 
            return ++Manager::counter; 
        } 
    static int counter; 
}; 

int Manager::counter;

template<class T> int ticket(); // <-- this should work with a conformant compiler  

int main()
{
  Manager m;
  std::cout << "ticket: " << ticket<int>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

希望有帮助:)


Joh*_*itb 8

修复

有一个热门修复程序,但如果您想了解正在发生的事情,请阅读以下说明.

#include <iostream>

template<typename T> int ticket();

class Manager { 
public:
    template<typename T> 
        friend int ticket() { 
            return ++Manager::counter; 
        } 
    static int counter; 
}; 

int Manager::counter; // don't forget the definition

int main() {
    Manager m;
    std::cout << "ticket: " << ticket<int>() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如代码段所示,您必须声明模板,以便在调用时使其可见.

朋友的功能定义

这很令人困惑,因为在这种情况下有一些规则.一些基本点,然后一些其他点.

struct A {
    friend void f(A*) { std::cout << "hello"; }
};
Run Code Online (Sandbox Code Playgroud)

它有什么作用?它定义了一个友元函数.这样的函数是封闭命名空间的成员.它不是类成员,即使它是在类中定义的!它在类中定义的事实只会改变该函数的词法范围:它可以直接引用该类的成员,而不会在类名之前.

但最重要的是,该函数在声明后不可见.例如,你不能把它的地址做成这样的事情

&f
Run Code Online (Sandbox Code Playgroud)

函数唯一可行的方法是使用参数依赖查找.最终将该类作为其关联类的查找将考虑该友元函数.这意味着以下工作:

f((A*)0);
Run Code Online (Sandbox Code Playgroud)

它的工作原理是因为调用包含一个带有类所包含类型的参数.在这种情况下,该类是一个关联的类,将考虑朋友声明.

例如,以下内容不起作用

f(0);
Run Code Online (Sandbox Code Playgroud)

因为它不知道它应该在A寻找朋友声明.将找不到没有参数的函数的友元函数定义,因为没有参数依赖查找发生.

模板的朋友功能定义

除了您的调用不包含参数这一事实外,它还有另一个问题.如果定义好友功能模板,则问题更复杂.有一条规则说,如果编译器看到T<A1, A2, A3>,如果T实际解析为模板,则这仅指模板特化.考虑

ticket < int > ()
Run Code Online (Sandbox Code Playgroud)

编译器无法解析ticket,因为它对正常查找不可见.为此,该规则表示,ticket<int>没有指功能.它必须被解析为关系表达式,产生以下结果

(ticket < int) > ()
Run Code Online (Sandbox Code Playgroud)

这将是语法错误,因为int它不是值,()也不是值.

这是一个重要的例子.

struct A {
    template<int I> friend void f(A*) { }
};

// try to comment this out
template<typename Dummy> void f();

int main() {
    f<1>((A*)0);
}
Run Code Online (Sandbox Code Playgroud)

编译.它编译是因为f解析为一个模板(尽管一个完全不同的模板甚至不能接受非类型模板参数 - 但这并不重要!).但是,一旦你注释掉第二个声明,标准符合编译器将不会编译片段,因为它被编译为关系表达式(小于和小于)并且它将找不到符号f.

阅读此主题以获取更多信息:我在这个模板玩具示例中缺少什么?.