如何调用在类中定义的好友模板函数? [英] How do I call a friend template function defined inside a class?

查看:53
本文介绍了如何调用在类中定义的好友模板函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从书中得到了这个示例,但是我不知道如何实际调用票证功能.这是代码:

I got this example from my book, but I have no idea how to actually call the ticket function. This is the code:

#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;
}

我得到无法访问的候选功能",错误消息.

I get the "candidate function(s) not accessible" error message.

推荐答案

以下几点将帮助您弄清这里发生的事情:

A few points will help you figure out what's going on here:

I)类中的Friend函数定义只能在从类定义之外调用时通过与参数相关的查找来找到.

I) Friend function definitions within classes can only be found by Argument dependent lookup when called from outside the class definition.

II)提供的显式模板参数的功能模板不会接受ADL,除非向编译器提供了一些明确的帮助,以将调用识别为函数调用.

II) Function templates that are supplied explicit template arguments do not undergo ADL unless the compiler is given some explicit help in identifying the call as a function call.

III)依赖于参数的查询(ADL)仅适用于用户定义的类型.

III) Argument dependent lookup (ADL) only works for user defined types.

一些示例可以更好地说明上述各点:

A few examples will better illustrate each of the above points:

//------------------------
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示例,现在这应该很简单:

Ok so now lets return to the Vandevoorde example that you quoted, and now this should be easy:

#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;
}

希望有帮助:)

这篇关于如何调用在类中定义的好友模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆