内联朋友函数的范围是什么? [英] What's the scope of inline friend functions?
问题描述
在搜索aroung SO后,有一个问题告诉我,内联好友函数的词汇范围是它定义的类,这意味着它可以访问。 typedef
在类中没有限定他们。但是后来我想知道这样一个函数的实际范围是多少? GCC至少拒绝所有我调用它的尝试。可以通过除ADL之外的其他方式调用函数,例如在这个例子中,这是不可能在这里由于没有参数?
标准报价是赞赏,因为我目前无法访问我的副本。
命名空间foo {
struct bar {
friend void baz(){}
void call_friend ;
};
}
int main(){
foo :: baz(); //无法通过包含类的范围访问
foo :: bar :: baz(); //不能通过类作用域访问
}
命名空间foo {
void bar :: call_friend(){
baz(); //无法通过成员函数访问
}
}
这些错误:
prog.cpp:在函数'int main()':
prog.cpp:9:错误:'baz'不是'foo'的成员
prog.cpp:10:错误:'baz'不是'foo :: bar'的成员
prog.cpp:在成员函数'void foo :: bar :: call_friend()':
prog.cpp:15:error:'baz'未在此范围内声明
在类中声明一个 friend
如果该函数以前没有被声明,则 friend
声明不会使该函数在正常查找的范围中可见。
这是在许多注释中强调的,但是确定的语句在7.3.1.2/3(ISO / IEC 14882:2011):
命名空间中首次声明的每个名称都是该命名空间的成员。如果非局部类第一中的
friend
声明声明类或函数,则friend类或函数是最内层命名空间的成员。 通过无限制查找(3.4.1)或限定查找(3.4.3)找到朋友的姓名,直到在该命名空间范围中提供匹配的声明(在类定义之前或之后)授予友谊)。如果调用了一个friend函数,它的名称可以通过名称查找找到,该名称查找考虑来自与函数参数类型相关联的命名空间和类的函数(3.4.2)。如果friend
声明中的名称既不合格,也不是 template-id ,声明是函数或 elaborated-
After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it's defined in, meaning it can access e.g. the typedef
s in the class without qualifying them. But then I wondered what is the actual scope of such a function? GCC at least rejects all my attempts to call it. Can a function such as in the example ever be called through means other than ADL, which is not possible here thanks to no arguments?
Standard quotations are appreciated, as I currently can't access my copy of it.
namespace foo{
struct bar{
friend void baz(){}
void call_friend();
};
}
int main(){
foo::baz(); // can't access through enclosing scope of the class
foo::bar::baz(); // can't access through class scope
}
namespace foo{
void bar::call_friend(){
baz(); // can't access through member function
}
}
results in these errors:
prog.cpp: In function ‘int main()’:
prog.cpp:9: error: ‘baz’ is not a member of ‘foo’
prog.cpp:10: error: ‘baz’ is not a member of ‘foo::bar’
prog.cpp: In member function ‘void foo::bar::call_friend()’:
prog.cpp:15: error: ‘baz’ was not declared in this scope
When you declare a friend
function with an unqualified id in a class it names a function in the nearest enclosing namespace scope.
If that function hasn't previously been declared then the friend
declaration doesn't make that function visible in that scope for normal lookup. It does make the declared function visible to argument-dependent lookup.
This is emphasised in many notes, but the definitive statement is in 7.3.1.2/3 (of ISO/IEC 14882:2011):
Every name first declared in a namespace is a member of that namespace. If a
friend
declaration in a non-local class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2). If the name in afriend
declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace.
这篇关于内联朋友函数的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!