【问题标题】:How do I call a friend template function defined inside a class?如何调用类中定义的朋友模板函数?
【发布时间】:2023-03-25 21:05:01
【问题描述】:

我从书中得到了这个例子,但我不知道如何实际调用票证函数。这是代码:

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

我收到“无法访问候选函数”错误消息。

【问题讨论】:

  • 这必须是模板函数有什么原因吗?看起来您实际上并没有在任何地方使用类型 T。
  • 我同意 Piotr 的观点,即拥有编译器名称和版本会有所帮助。

标签: c++ templates


【解决方案1】:

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

I) 当从类定义外部调用时,类中的友元函数定义只能通过依赖于参数的查找来找到。

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

希望对你有帮助:)

【讨论】:

  • 我不清楚为什么int l = g(s); 应该起作用。编译器如何推断模板参数?
【解决方案2】:

修补程序

有一个可用的热修复程序,但如果您想了解发生了什么,请阅读以下说明。

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

正如 sn-p 所示,您必须声明模板以使其在调用时可见。

好友函数定义

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

struct A {
    friend void f(A*) { std::cout << "hello"; }
};

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

不过,最重要的是,函数在声明后是不可见的。你不能拿它的地址做这样的事情,例如

&f

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

f((A*)0);

之所以有效,是因为该调用包含一个类型包含该类的参数。在这种情况下,该类是一个关联类,并且会考虑友元声明。

例如下面的行不通

f(0);

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

模板的好友函数定义

除了您的调用不包含参数这一事实之外,它还有另一个问题。如果定义友元函数模板,事情就复杂了。有一条规则说,如果编译器看到T&lt;A1, A2, A3&gt;,那么如果T 实际上解析为模板,则这仅指模板特化。考虑

ticket < int > ()

编译器无法解析ticket,因为它对正常查找不可见。因此,规则说ticket&lt;int&gt; not 指的是一个函数。它必须被解析为关系表达式,产生以下结果

(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 解析为一个模板(尽管是一个完全不同的模板,它甚至不能接受非类型模板参数——但这没关系!)。但是,一旦您注释掉第二个声明,符合标准的编译器将不会编译 sn-p,因为它被编译为关系表达式(小于和小于)并且它不会找到符号 f

阅读此主题以获取更多信息:What am I missing in this template toy example?

【讨论】:

    【解决方案3】:

    我在使用 MS VS++ 编译器时确实遇到了同样的错误。根据 MSDN 上的文档:

    http://msdn.microsoft.com/en-us/library/h2x4fzdz(VS.80).aspx

    朋友不在班级范围内, 并且不使用 成员选择运算符(. 和 –>) 除非他们是另一个成员 班级。一个友元函数被声明 由授予访问权限的类。

    所以友元函数实际上不是类的一部分,因此不应在类范围内定义。在类外定义函数:

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

    【讨论】:

      【解决方案4】:

      为什么不改成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;
      }
      

      虽然真的,它也不一定是模板。我假设您需要有关朋友模板的具体信息?

      【讨论】:

      • 是的,我正在学习如何定义朋友函数模板,该类可以编译,但是当我尝试实际调用它时,我不能,我正在使用 VS 9 的试用版,非常感谢大家的帮助:]跨度>
      • 我刚刚在 code::blocks 上尝试了 Piotr Findeisen 的代码,并且编译良好,非常感谢你们:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      相关资源
      最近更新 更多