C++ primer:16.4.3. 类模板中的友元声明 - 声明依赖性

来源:互联网 发布:张俪的淘宝店 编辑:程序博客网 时间:2024/05/16 07:53

当授予对给定模板的实例的访问权时候,在作用域中不需要存在该类模板或函数模板的声明。实质上,编译器将友元声明也当作类或函数的声明对待(When we grant access to all instances of a given template, there need not be a declaration for that class or function template in scope. Essentially, the compiler treats the friend declaration as a declaration of the class or function as well.)。 

想要限制对特定实例化的友元关系时,必须在可以用于友元声明之前声明类或函数(When we want to restrict friendship to a specific instantiation, then the class or function must have been declared before it can be used in a friend declaration:):想要将友元关系限定在特定的实例化上,则相关的类或函数必须在其友元声明之前进行声明。

如果没有事先告诉编译器该友元是一个模板,则编译器将认为该友元是一个普通非模板类或非模板函数(If we have not previously told the compiler that the friend is a template, then the compiler will infer that the friend is an ordinary nontemplate class or function.)。

原创粉丝点击