大家好,请帮我这个功能 我从我的书中得到了这个例子,但我不知道如何实际调用票函数 这是代码:
#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;
}
我得到“候选功能无法访问”错误消息 非常感谢!
答案 0 :(得分:13)
有几点可以帮助你弄清楚这里发生了什么:
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.
好的,现在让我们回到您引用的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;
}
希望有帮助:)
答案 1 :(得分:7)
有一个热门修复程序,但如果您想了解正在发生的事情,请阅读以下说明。
#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;
}
如代码段所示,您必须声明模板,以便在调用时使其可见。
这很令人困惑,因为在这种情况下会有一些规则。一些基本点,然后一些其他点。
struct A {
friend void f(A*) { std::cout << "hello"; }
};
它做什么?它定义了一个友元函数。这样的函数是封闭命名空间的成员。它是不是类成员,即使它是在类中定义的!它在类中定义的事实只会改变该函数的词法范围:它可以直接引用该类的成员,而不会在类名之前。
但最重要的是,该功能在声明后不可见。你不能把它的地址做成这样的事情,例如
&f
该函数唯一可行的方法是使用参数依赖查找。最终将该类作为其关联类的查找将考虑该友元函数。这意味着以下工作:
f((A*)0);
它的工作原理是因为调用包含一个带有类所包含类型的参数。在这种情况下,该类是一个关联的类,将考虑朋友声明。
以下内容无效,例如
f(0);
因为它不知道它应该在A
内寻找朋友声明。将找不到没有参数的函数的友元函数定义,因为没有参数依赖查找发生。
除了您的调用不包含参数这一事实外,它还有另一个问题。如果您定义了朋友功能模板,那么问题就更复杂了。有一条规则说,如果编译器看到T<A1, A2, A3>
,那么只有在T
实际解析为模板时才会引用模板专门化。考虑
ticket < int > ()
编译器无法解析ticket
,因为它对正常查找不可见。因此,规则说ticket<int>
不是引用函数。它必须被解析为关系表达式,产生以下
(ticket < int) > ()
这将是语法错误,因为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);
}
编译。它编译是因为f
解析为模板(尽管一个完全不同的模板甚至不能接受非类型模板参数 - 但这并不重要!)。但是,一旦你注释掉第二个声明,标准符合编译器将不会编译片段,因为它被编译为关系表达式(小于和小于)并且它将找不到符号f
。
阅读此主题以获取更多信息:What am I missing in this template toy example?。
答案 2 :(得分:4)
我使用MS VS ++编译器得到了同样的错误。根据MSDN上的文档:
http://msdn.microsoft.com/en-us/library/h2x4fzdz(VS.80).aspx
朋友不属于班级范围, 他们没有使用 成员选择运算符(。和 - &gt;) 除非他们是另一个人的成员 类。声明了友元函数 由授予访问权限的类。
所以朋友函数实际上并不属于类,因此不应该在类范围内定义。定义类外的函数:
#include <iostream>
class Manager {
public:
template<typename T>
friend int ticket();
static int counter;
};
template<typename T>
int ticket() {
return ++Manager::counter;
}
int Manager::counter;
int main()
{
Manager m;
std::cout << "ticket: " << ticket<int>() << std::endl;
}
答案 3 :(得分:0)
为什么不改为static
?
#include <iostream>
class Manager {
public:
template<typename T>
static int ticket()
{
return ++Manager::counter;
}
static int counter;
};
int main()
{
Manager m;
std::cout << "ticket: " << Manager::ticket<int>() << std::endl;
}
尽管如此,它也不一定是模板。我假设您需要有关朋友模板的具体信息吗?