加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:55:24 所属栏目:百科 来源:网络整理
导读:嗨,大家好,请帮我这个功能 我从我的书中得到了这个例子,但我不知道如何实际调用票函数 这是代码: #include iostream class Manager { public: templatetypename T friend int ticket() { return ++Manager::counter; } static int counter; }; int main(){
嗨,大家好,请帮我这个功能
我从我的书中得到了这个例子,但我不知道如何实际调用票函数
这是代码:

#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)类中的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;
}

希望有帮助:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读