朋友声明:这是clang中的bug吗
Friend declarations: Is this a bug in clang?
§3.4.1/3有以下示例:
typedef int f;
namespace N
{
struct A
{
friend void f(A &);
operator int();
void g(A a)
{
int i = f(a);
}
};
}
由于编译器在声明int i = f(a);
中的名称f
的普通查找中没有看到友元声明N::A::f(A&)
,因此编译时没有出现错误(参见实际示例),根据§7.3.1.2/3,这是正确的。
§7.3.1.2/3(重点是我的):
首先在命名空间中声明的每个名称都是命名空间。如果非局部类中的友元声明首先声明类或函数友元类或函数是最里面的封闭命名空间。找不到朋友的名字不合格查找(3.4.1)或通过合格查找(3.4.3)直到在该命名空间范围中提供匹配声明(在授予友谊的类定义之前或之后)。
现在,如果我们包括声明
struct A;
void f(A&);
在namespace N
中的上述代码段中,在struct A
之前,代码将正确地发出错误(参见实际示例),因为现在函数::f(A&)
是通过在表达式int i = f(a);
中查找名称f
来找到的,这符合§7.3.1.2/3。
但是,如果我们包括申报
void f(A&);
在namespace N
中,在struct A
之后,代码出人意料地没有发出任何错误,请参阅实际示例。
您误读了本规范摘录的上下文。这一段所传达的唯一内容涉及友元函数声明和它们的目标函数如何相互关联它与调用该特定函数的常规规则无关,在这方面也没有任何变化
因此,编译不会失败,因为函数f()
的原型在您尝试使用它时尚未声明(相反,它会找到具有相同名称的typedef)。
如果您修改代码以在类之外实现A::g()
成员,您会注意到,当f()
原型出现在实现之前时编译失败,而当原型出现在它之后时编译成功——这正是如果没有友元声明时所期望的情况。
在您引用的同一段(§7.3.1.2/3
)中,有一个示例澄清了查找规则:
// Assume f and g have not yet been defined.
void h(int);
template <class T> void f2(T);
namespace A {
class X {
friend void f(X); // A::f(X) is a friend
class Y {
friend void g(); // A::g is a friend
friend void h(int); // A::h is a friend
// ::h not considered
friend void f2<>(int); // ::f2<>(int) is a friend
};
};
// A::f, A::g and A::h are not visible here
X x;
void g() { f(x); } // definition of A::g
void f(X) { /* ... */} // definition of A::f
void h(int) { /* ... */ } // definition of A::h
// A::f, A::g and A::h are visible here and known to be friends
}
因此编译器工作正常。
- GCC Bug,使用尖括号通过宏包含标头时"linux"路径中的字符串"<>"替换为 "1"
- 我的erase()函数中的bug用于链接列表
- 仅限版本中的Bug
- 签名:GCC 中的 NESS 优化"bug"
- 以下错误 C2248 的 bug 搜寻提示:'std::basic_ios<_Elem,_Traits>::basic_ios':无法访问类中声明的私有成员
- 朋友声明:这是clang中的bug吗
- c++中的Bug ?c++中的-set_difference不返回std::copy
- gcc7的std::变体实现中的Bug
- for循环中的奇怪行为-一个bug
- 可变模板元编程:clang++或g++中的一个bug
- 这是GCC中的一个bug吗?
- CLang中的enable_if bug (bug 11723)是否有更好的解决方案?
- 我的展开树实现中出现了奇怪的bug
- 基本递归程序中的Bug,可以看出问题所在吗
- c++代码中的Bug可能是指针没有指向任何东西
- libc++ std::search_n中的崩溃是一个bug吗?
- gcc的Bug?模板类中友元函数的访问控制问题
- 这是g++中的一个bug吗?
- visual studio -这(崩溃)是VS2012 c++编译器中的一个bug吗?
- 这是Mac OS X 10.6 c++ std API中的一个bug吗?